C# Case Insensitive String Compare
Generic Type Conversion
This method can be used to convert the passed in object to a specified type.
internal static T Cast(object o)
{
T value;
try
{
value = (T)o;
}
catch
{
value = default(T);
}
return value;
}
Unknown Generic Type cannot be NULL and default(T)
Lets say we are creating a generic method of type T used to convert one type into another. That being said there might be a type conversion exception thrown within the method, if this happens we want to simply return a null indicating that the conversion did not happen correctly. Unfortunately, generic types cannot be set to null, this is because a generic type could be a reference type or a value type depending on what is passed in. Because of this possibility and the fact that value types cannot be implicitly converted to null you must use a new ‘default’ keyword which checks the passed in type to see if it evaluates to a reference type or a value type, if a reference type the passed in type is set to null, if a value type it is set to the default value for that value type.
C# GZip/Zip Compression/Decompression
Simple zip compression in C#:
private static void ZipCompress()
{
const string srcFile = “DataFile.xml”;
const string desFile = “Compressed.zip”;
FileStream fsIn = null;
FileStream fsOut = null;
GZipStream gzip = null;
byte[] buffer;
try
{
fsOut = new FileStream(desFile, FileMode.Create, FileAccess.Write, FileShare.None);
gzip = new GZipStream(fsOut, CompressionMode.Compress, true);
fsIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
buffer = new byte[fsIn.Length];
fsIn.Read(buffer, 0, buffer.Length);
fsIn.Close();
gzip.Write(buffer, 0, buffer.Length);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Assert(false, ex.ToString());
}
finally
{
if (gzip != null)
gzip.Close();
if (fsOut != null)
fsOut.Close();
if (fsIn != null)
fsIn.Close();
}
}
Simple zip decompression in C#:
private static void ZipDecompress()
{
const string srcFile = “Compressed.zip”;
const string desFile = “Decompressed.xml”;
FileStream fsIn = null;
FileStream fsOut = null;
GZipStream gzip = null;
const int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];
try
{
fsIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
fsOut = new FileStream(desFile, FileMode.Create, FileAccess.Write, FileShare.None);
gzip = new GZipStream(fsIn, CompressionMode.Decompress, true);
while (true)
{
int count = gzip.Read(buffer, 0, bufferSize);
if (count != 0)
fsOut.Write(buffer, 0, count);
if (count != bufferSize)
break;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Assert(false, ex.ToString());
}
finally
{
if (gzip != null)
gzip.Close();
if (fsOut != null)
fsOut.Close();
if (fsIn != null)
fsIn.Close();
}
}
C# Singleton Pattern
The Singleton Design Pattern is the standard way programmers create classes which are designed to have one and only one instantiation at any point in time. These classes can be used for configurations or settings objects which are globally available and which need to maintain state when being read or updated by various portions of the application.
This is accomplished by making the class Constructor private so that it cannot be accessed. This stops the class from being instantiated by code external to the class itself. Then a public property is exposed which retrieves a reference to an existing instance of the class stored in a local variable or if an instance does not yet exist the class creates a new instance and saves the reference for later calls.
Example Code:
