Monday, 1 April 2013

Reference Type and Value Type Variables


It always pays to know variable-types better. Some are mutable, and some are immutable, and what not!..

Here, we’ll perform an equality test on different types of variables using the == operator, Equals function, and the ReferenceEquals functionality.


The following write-up describes all the concepts I go through during the video-demo.

First-up, we’ll declare pairs of similar-type variables holding identical values. The types declared here are string, int and object holding int, string, and class type.

Next, I go about explaining the basics of Heap and Stack memory, and the pros & cons between them with suitable examples.

In the debriefing on the == operator, I talk about their default overloads. I also shed some light on why strings are immutable and what the String Literal Pool optimization does.

While going-through Equals function, the similarity with the == operator is discussed. Apart from the overloads, the Equals function has overrides by precedence, object.Equals(object) being the last resort performing a reference-equality check rather than a value-equality check. Also, the susceptibility to System.NullReferenceException for Equals function is mentioned.

ReferenceEquals functionality is fairly easier to explain having gone through the concepts of mutability, immutability and string interning.

For a deeper understanding on all that has been mentioned above, please check out the 5 video links where I go through the demo in full detail.









The code typed-in during the tutorial-session is as follows:-
        class Person
        {
            public Person(double h, double w)
            {
                height = h;
                weight = w;
            }

            public double height { get; set; }
            public double weight { get; set; }
        }

             //111
            //string var1 = "hi";
            //string var2 = "hi";

            //111
            //object var1 = "hello";
            //object var2 = "hello";

            //110
            //int var1 = 5;
            //int var2 = 5;

            //010
            //object var1 = 2;
            //object var2 = 2;

            //000
            Person var1 = new Person(72, 64);
            Person var2 = new Person(72, 64);

            //Value-Type Variable --> Stored in Stack
            //Reference-Type Variable --> Stored in Heap

            //Stack -> Last-in First-out memory == First-in Last-out memory
            //Fast and Limited Memory. For Function's contents. Automatic Deallocation
            //Eg: int, double, byte, etc.

            //Heap --> Dynamically Allocation. Slow & Large Memory.
            //Grabage collected!
            //Eg: string, object and Collections like Lists, Dictionaries, etc.

            StringBuilder sb = new StringBuilder();

            // == works due to Operator Overloading
            //By default, Value-Types are compared by their value (contents)
            //By default, Reference-Types are compared by their address equality.
            //By default, the string reference-type is compared by its contents.

            string s1 = "hi";
            string s2 = "hi";
            //s2 = "hello";

            //Strings are immutable reference types. Interning in .NET
            //This means that 2 string constants with the same value will refer to the
            //same object instance at runtime.
            //LITERALS. String Literal Pool
            //Runtime strings (or strings in a collection will be in General Heap)
            if (var1 == var2)//1-0
                sb.Append(String.Format("TYPE: {0}. \n {1} & {2} are equal when compared with ==.", var1.GetType(), var1, var2));
            else
                sb.Append(String.Format("TYPE: {0}. \n {1} & {2} are not equal when compared with ==.", var1.GetType(), var1, var2));

            //Equals is very similar to the == overloaded operator.
            //Equals function has many overloads (oject.Equals(object), etc)
            //Equals function has many overrides by precedence. The last in precedence is object.Equals(object)
            //By default, Value-types are compared by their value
            //By default. Reference-types are compared by their Address Equality.
           
            //== is resistant to null.
            //Equals throws a System.NullReferenceException
            if (var1.Equals(var2))//1-0
                sb.Append(String.Format("\n {0} & {1} are equal when compared with Equals.", var1, var2));
            else
                sb.Append(String.Format("\n {0} & {1} are not equal when compared with Equals.", var1, var2));

            if (object.ReferenceEquals(var1, var2))//1-0
                sb.Append(String.Format("\n {0} & {1} have the same Reference Address.", var1, var2));
            else
                sb.Append(String.Format("\n {0} & {1} have different Reference Address.", var1, var2));

            MessageBox.Show(sb.ToString());


Thank you for reading this post and watching the videos. Please Subscribe, Comment, and Rate the channel if you liked the videos.

Goto C# Experiments to access more of such content! Thanks again!