This is one of those things that is so obvious I wonder why it never occured to me before! If you declare a member as static the compiler only creates one member instance across all class instances.
class ClassWideStatic
{
protected static int NoOfInstances;
public ClassWideStatic()
{
//setting NoOfInstances here will set it on ALL instances
//effectively resetting it, but modifying it is fine
NoOfInstances += 1;
}
static ClassWideStatic()
{
//This is a special constructor that is only called when
//the first instance is created
NoOfInstances = 1;
}
}
When we create a class we get given a default blank constructor which is provided by the framework. Its equivalent to
We can create constructors with different parameters:
public ClassName(int One)
{
this.One = One;
}
public ClassName(int One, string Two)
{
this.One = One;
this.Two = Two;
}
Read more…
Defining the Flags:
To start of with, declare an enum to list all the possible flags. Two things are important when declaring the enum. The first thing you will probably notice is the [Flags] attribute. This is necessary in order to indicate that the enumeration should be treated as a set of flags. The second important thing is assigning a value to each of the items in the enum. The first value should be 1, then just double the value for each consecutive item. The integer type in .NET can store up to 32 flags. Read more…
There is a DLL called CommonAssembly. This contains a namespace called CommonNamespace. This namespace contains a class called CommonClass.
Another project adds a reference to CommonAssembly and wants to get the type of CommonClass using the method Type.GetType.
Read more…
typeof(IFoo).IsAssignableFrom(bar.GetType());
typeof(IFoo).IsAssignableFrom(typeof(BarClass));
The Type.IsAssignableFrom() method check whether a type can be assigned from another. It takes a System.Type object as argument, so if you are given a class or an instance of a class, you can use the typeof operator or the Object.GetType() method to get the corresponding System.Type object respectively. The MSDN has a clear definition of the returned value of the IsAssignableFrom() method:
Read more…
Original Article
I couldn’t find a quick reference to .NET string formatting using the String.Format() function, so I created this one (which has also spawned this String Formatting FAQ and strangely enough, this cartoon. Read more…