I started learning C sharp from Andrew Tolson's Pro C# 5.0 and the .NET 4.5 Framework, 6th Edition a few days back and as a Java Programmer, many concepts have embezzled me regarding the Type concept in C sharp.
I was reading enumeration in C sharp and I saw the type hierarchy of System. Basically, author has said that the enumerations are extended from System.ValueType abstract class. From this I inferred enum is nothing but an extension of the class ValueType. I also examined the IL for an enum and it was something like this:
.class private auto ansi sealed TestPrograms.EmpType
extends [mscorlib]System.Enum
{
} // end of class TestPrograms.EmpType
.field public static literal valuetype TestPrograms.EmpType Contractor = uint8(0x64)
//...and so on for other literals
for the following enum,
enum EmpType : byte
{
Manager = 10,
Grunt = 1,
Contractor = 100,
VicePresident = 9
}
Now here is the thing - Since EmpType is a class extension of System.Enum, writing the following should mean a class reference,
EmpType emp;
My first question is as follows:
Why do we initialize this reference as,
EmpType emp = EmpType.Contractor;
And my second question is, if I pass this reference as a method argument, the resulting behavior is "Call by Value", i.e, if I write the following code:
class EmpTypeTest
{
static void Main(string[] args)
{
Console.WriteLine("**** Fun with Enums *****");
EmpType emp = EmpType.Contractor;
AskForBonus(emp);
Console.WriteLine((byte)emp);
// Prints out "emp is a Contractor".
Console.WriteLine("emp is a {0}.", emp.ToString());
Console.ReadLine();
}
static void AskForBonus(EmpType e)
{
switch (e)
{
case EmpType.Manager:
Console.WriteLine("How about stock options instead?");
break;
case EmpType.Grunt:
Console.WriteLine("You have got to be kidding...");
break;
case EmpType.Contractor:
Console.WriteLine("You already get enough cash...");
e++;
Console.WriteLine((byte)e);
break;
case EmpType.VicePresident:
Console.WriteLine("VERY GOOD, Sir!");
break;
}
}
}
there is no change in the value of emp. Why isn't the "Call by reference" behavior supported?