C# Security – Using Hashing

Hashing   is a method of one-way encryption which is ideal for storing passwords in a database, as you may never require a decrypted version. To authenticate some data, simply hash what the user input and compare it with the data stored in the database.
A hash code will always be a small fixed size irrespective [...]

Read more

Get the OS, Service Pack, and CLR Version info using C#

You can use the System.Environment.OSVersion class to access detail s of the user’s system environment
The below code demonstrates using the OSVersion object to access environmental info:

OperatingSystem osObj = System.Environment.OSVersion;
Console.WriteLine(“Platform is: {0}”, osObj.Platform);
Console.WriteLine(“Service Pack is: {0}”, osObj.ServicePack);
Console.WriteLine(“Version is: {0}”, osObj.Version);
Console.WriteLine(“VersionString is: {0}”, osObj.VersionString);
Console.WriteLine(“CLR Version is: {0}”, System.Environment.Version);

Note that the OSVersion object will [...]

Read more

Use ReaderWriterLockSlim to Protect Access to Resources in C#

It is normally safe to allow let multiple threads read data at the same time, but when a thread is required to perform a write, all other threads will need to be blocked. The .NET framework originally provided ReaderWriterLock for this circumstance, but it has performance issues which often outweigh its usefulness. [...]

Read more

Protect Data on Multiple Processes using C#

If you have a routine which splits data across processors using the Parallel class you may need to protect memory, files or other resources which are shared across the multiple processes. One solution to this is to use a mutex, which is similar to a Monitor object, but at the operating system level. Mutexes must [...]

Read more

Using the Parallel Class in C# – Run Tasks in Parallel on Multiple Processors or Cores

The Parallel Class can be used to split up the tasks which work on the data. For example if you have iterative code that looks like the below code:
//6 parts of the book
string[] inputTextFiles =
{
“part1.txt”, “part2.txt”, “part3.txt”,
“part4.txt”, “part5.txt”, “part6.txt”
};

foreach (string file in inputTextFiles)

{
string contentStr = File.ReadAllText(file);
CountCharacters(contentStr);
CountWords(contentStr);
}
The Parallel class can be used with lambda expressions [...]

Read more

Using the Parallel Class in C# – Processing Data in Parallel across Multiple Processors or Cores

The new Task Parallel Library introduced with .NET 4, allows you to easily split code execution onto multiple processors without using threads or locks.
For a task which is easily split into independent sub-tasks, or data that can be partitioned and computed separately you can use the Parallel class in System.Threading to assign [...]

Read more

Instantiate a Class Dynamically in C#

You can dynamically instantiate a class in C# using reflection, it’s possible to instantiate code from assemblies which are not referenced at build-time. For example, if you had class defined in DynamicInstantiateLb.dll:
public class DynamicTestClass
{
public int Add(int x, int y)
{
return x + y;
}
public string CombineStrings(T x, T y)
{
return x.ToString() + “, “ + y.ToString();
}
}
In a separate [...]

Read more

C# Garbage Collecting – Destructors Versus Dispose

In C# it is  illegal to call a destructor explicitly it must be called by the garbage collector. If you handle  unmanaged resources  that need to be closed and disposed of as soon as possible, you should implement the IDisposable interface.   IDisposable requires implementers to define one method, named Dispose(), to perform [...]

Read more

C# Destructor

As C# has an inbuilt garbage collection it is not necessary to explicitly destroy your objects. However, for objects which control unmanaged resources, these resources need to be explicitly freed when they are no longer require. In C#, implicit control over unmanaged resources is provided by a destructor, which can be called by the garbage [...]

Read more

C# 4.0 – Covariance and Contravariance of Generics

Covariance allows casting of generic types to the base types, for example, IEnumerable<X> will be implicitly convertible an IEnumerable<Y> if X can implicitly be converted to Y.
// List of strings
IList<string> stringList = new List<string>();

[...]

Read more