How to check object instances are of a specific class or inherited from a specific class
You can determine if an object is of a specific class by using the typeof keyword. if (myObj.GetType() == typeof(MyClass)) { // Do something } If you want to check to see if an object is of a specific type, … Continue reading
How to override constructors and pass extra parameters
If you want to descend from a class that requires parameters passed in its constructor, and you wish to introduce extra parameters in the descendants constructor then when you define the derived classes constructor, simply add : base() to the … Continue reading
How to create a bit-based enumeration
If you want to create an enumeration that acts like a set, where the value of the enumeration can be a combination of any of the members of the enumeration then you C# allows you to do this using the … Continue reading
Custom Exceptions and Serialization.
Microsoft state that if you want to raise custom exceptions in your application then you should derive them from the ApplicationException class and not the Exception class. ApplicationException is thrown by a user program, not by the common language runtime. … Continue reading
Nullable Types
In C# and .net you can define a variable of the standard values types to be nullable as well. This is extremely useful in circumstances when you want to check if a varaible has been intialized correctly. For example, if … Continue reading
Hex String to byte array converter
The following simple static class will take a string of Hexdecimal characters and convert it to a byte array. static class HexStringConverter { public static byte[] ToByteArray(String HexString) { int NumberChars = HexString.Length; byte[] bytes = new byte[NumberChars / 2]; … Continue reading
How to create an Expandable Object Converter that will drop down a list of Class Types
This article provides an example of a TypeConverter which, when displayed in a Property Grid, will allow the user to select an class type from the drop down list. When an item is selected, it creates and instance of that … Continue reading
How to use an inline delegate with the Find method on collections
C# provides a very powerful and concise method of locating items in collections. The find Method on collections and generic lists (and other collection type classes) take a predicate as the parameter. To the beginner programmer the power of this … Continue reading
Create a string of Repeating Characters
Creating a string of repeating characters is really simple in c#, however it is not in the obvious place you would look. First I tried the String class, hoping there would be a static like String.RepeatString, and even looked in … Continue reading
Performance Iterating Generic Lists
There are 3 obvious ways of iterating through each item in a generic list, but which is the most efficient. 1. using a for statement 2. using a foreach statement 3. using the List.ForEach method with a delegate I created … Continue reading