1

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?

Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
  • As for the "call by reference" part, _all_ parameter passing in C# is "call by value" unless `ref` or `out` are specified. Please do not conflate passing a reference by value with passing "by reference". They are not the same. – Peter Duniho Feb 08 '15 at 09:18
  • @PeterDuniho : Yeah, I read the answer and now what I still want to ask is, see my comment to Christos' answer – Manish Kumar Sharma Feb 08 '15 at 09:22
  • `EmpType` is not a reference type. It is a value type. You need to read the [Ecma-335 CLI specification](http://www.ecma-international.org/publications/standards/Ecma-335.htm). All types that inherit from `System.ValueType` that are not `System.Enum` and `System.Enum` are value types. You are infering something from the decompiled IL that is simply not true. – Mike Zboray Feb 08 '15 at 09:25
  • @mikez : Ecma-335 CLI specification? Hmm...I knew the moment would come. The reason I had held off was, I started C sharp 5 days ago and am still going through the Core language constructs. So, I would read it after spending some more time with the language. But thanks for recommending – Manish Kumar Sharma Feb 08 '15 at 09:29
  • @mikez : Anyway mike, can ya tell me, which language part is reponsible for rendering the ValueType behavior to an enum? ValueType is just an abstract class with no other interface implementation – Manish Kumar Sharma Feb 08 '15 at 09:32
  • @pulp_fiction in C# the two ways to declare value type are using the `struct` and and `enum` keywords. The compiler transforms those into types that inherit from `System.ValueType` and `System.Enum`, respectively. You cannot not inherit from those types directly. For the purposes of learning the language, just take it as a given that that is how you declare value types. – Mike Zboray Feb 08 '15 at 09:41
  • Also as to the decompiled IL, the spec has this to say of type declarations which are called "class headers" in the spec: For historical reasons, many of the syntactic categories used for defining types incorrectly use “class” instead of “type” in their name. All classes are types, but “types” is a broader term encompassing value types, and interfaces as well. – Mike Zboray Feb 08 '15 at 09:43
  • "I started C sharp 5 days ago" -- FWIW I'd say that at this point in your learning, you would be much better served by reading the [C# Programming Guide](https://msdn.microsoft.com/en-us/library/67ef8sbd.aspx) on MSDN, including the sections on ["value types"](https://msdn.microsoft.com/en-us/library/s1ax56ch.aspx) and ["reference types"](https://msdn.microsoft.com/en-us/library/490f96s2.aspx), than to worry about specific implementation details. You may also find [Difference Between Passing a Struct and...a Class Reference](https://msdn.microsoft.com/en-us/library/8b0bdca4.aspx) useful – Peter Duniho Feb 08 '15 at 09:50
  • @PeterDuniho: You are right perhaps. But I am able to move forward through this book because of my Java background. Java's key language features and facilities are similar to C sharp. There are a few major differences and few minor ones here and there. Nevertheless, it has helped me a lot of things. – Manish Kumar Sharma Feb 08 '15 at 09:54
  • Java is similar. Just similar enough to _really_ confuse people at least some of the time. For example, `enum` in Java is significantly different from `enum` in C#. :) – Peter Duniho Feb 08 '15 at 10:03

0 Answers0