Delegates make it easier to find stuff in generic lists.
Let’s consider the following simple class:
-
Public Class Person{
-
private string m_name;
-
private int m_age;
-
-
// .Net3.0 syntax only.
-
public string Name{get;set}
-
public int Age{get;set}
-
-
public Person(string name, int age){
-
m_name = name;
-
m_age = age;
-
}
Instanciating :
-
List<Person> lstPpl = new List<Person>();
-
lstPpl.Add(new Person("Christian", 1));
-
lstPpl.Add(new Person("Noah", 0.3));
We can easily find Noah:
-
Person myson = lstPpl.Find(delegate(Person p) { return "Noah" == p.Name; });
For re-use: a small class that does encryption and binary serialization:
-
using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using System.IO;
-
using System.Runtime.Serialization.Formatters.Binary;
-
using System.Security.Cryptography;
-
using System.Windows.Forms;
-
-
namespace MyNamespace
-
{
-
class Tools
-
{
-
// change me…
-
private static string m_encryptionKey = "password";
-
-
public static byte[] Encrypt(byte[] plainData, string sKey)
-
{
-
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
-
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
-
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
-
ICryptoTransform desencrypt = DES.CreateEncryptor();
-
byte[] encryptedData = desencrypt.TransformFinalBlock(plainData, 0, plainData.Length);
-
return encryptedData;
-
}
-
-
public static byte[] Decrypt(byte[] encryptedData, string sKey)
-
{
-
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
-
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
-
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
-
ICryptoTransform desDecrypt = DES.CreateDecryptor();
-
byte[] decryptedData = desDecrypt.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
-
return decryptedData;
-
}
-
-
public static void SaveObjectToFile(object obj, string path){
-
try
-
{
-
MemoryStream memStream = new MemoryStream();
-
BinaryFormatter binFormatter = new BinaryFormatter();
-
binFormatter.Serialize(memStream, obj);
-
byte[] encryptedBytes = Encrypt(memStream.ToArray(), m_encryptionKey);
-
memStream.Close();
-
Stream streamToFile = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
-
streamToFile.Write(encryptedBytes, 0, encryptedBytes.Length);
-
streamToFile.Flush();
-
streamToFile.Close();
-
}
-
catch (Exception e)
-
{
-
MessageBox.Show(e.Message);
-
}
-
}
-
-
public static object LoadObjectFromFile(string path)
-
{
-
try
-
{
-
-
Stream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
-
byte[] encryptedObj = new byte[fileStream.Length];
-
fileStream.Read(encryptedObj, 0, (int)encryptedObj.Length);
-
MemoryStream memStream = new MemoryStream(Decrypt(encryptedObj, m_encryptionKey));
-
BinaryFormatter binFormatter = new BinaryFormatter();
-
object decryptedObj = binFormatter.Deserialize(memStream);
-
memStream.Close();
-
fileStream.Close();
-
return decryptedObj;
-
}
-
catch (Exception e)
-
{
-
MessageBox.Show(e.Message);
-
}
-
return null;
-
}
-
}
-
}