Types in .Net - Part 2
Wednesday, April 23rd, 2008In a previous article “Types in .Net - Part 1“, we looked at Built In Value Types. In this part we will look at User Defined Value Types.
User Defined Value Types:
User Defined Value Types are called structs. ’struct’ is the keyword we use in C# to create them. You can define them just like a class as in:
public struct MyStruct
{
}
You cant inherit a struct. Structs are Value Types not reference types like classes ie they are created on the stack not on heap.
The other type of Value Type is enum. The following code shows how to define enum’s.
public enum MyEnum
{
Value1,
Value2,
Value3
}
Enum’s are actually Integers, but when you do a .ToString() on them you get the representation like “Value1″. Enums are useful when programming and when you have to force to pick up from a certain list of values.
You use enum as follows:
MyEnum myEnum = MyEnum.Value1;
In a later article we will take a look at Reference types, which would be the concluding article to this series.









