Example #1
0
        static void StringEquality()
        {
            string a = "asdf";
            string b = "asdf";

            Console.WriteLine(a == b);

            int n1 = 5;
            int n2 = n1; // n2 gets a copy of the value of n1

            var dummy1 = new Dummy();
            var dummy2 = dummy1;

            dummy1.Data = 10;
            if (dummy2.Data == 10)
            {
                Console.WriteLine("reference type");
            }
            else
            {
                Console.WriteLine("value type");
            }

            var vDummy1 = new ValueDummy();
            var vDummy2 = vDummy1;

            vDummy1.Data = 10;
            if (vDummy2.Data == 10)
            {
                Console.WriteLine("reference type");
            }
            else
            {
                Console.WriteLine("value type");
            }

            // reference types need to be "garbage collected" because we don't know right away
            // when the LAST variable pointing to it has passed out of scope

            Console.WriteLine(new Dummy() == new Dummy());

            int    i1 = 5;
            object o2 = i1;
            // This is called "boxing" - the int is wrapped inside a reference type

            int i2 = (int)o2; // extract that value from inside the object wrapper
        }
Example #2
0
        static void StringEquality()
        {
            string a = "asdf";
            string b = "asdf";

            Console.WriteLine(a == b); // returns true

            // value types and reference types.
            // value type variables store their values directly.
            // reference type variables store a reference to their value.

            // in C#, many of our basic types are value types:
            // int, double, bool, float, long

            int n1 = 5;
            int n2 = n1; // int is value type, so n2 is a copy of n1

            var dummy1 = new Dummy();
            var dummy2 = dummy1;

            dummy1.Data = 10;
            if (dummy2.Data == 10)
            {
                Console.WriteLine("reference type");
            }
            else
            {
                Console.WriteLine("value type");
            }
            // Dummy is a reference type, so dummy2 is a copy of the reference,
            // i.e. a new reference to the same object.

            // objects made from classes are reference types, always
            // objects made from structs are value types.
            // all the built-in value types are "structs" in C#.


            var vDummy1 = new ValueDummy();
            var vDummy2 = vDummy1;

            vDummy1.Data = 10;
            if (vDummy2.Data == 10)
            {
                Console.WriteLine("reference type");
            }
            else
            {
                Console.WriteLine("value type");
            }

            // structs are copied entirely every time we pass it to a new method
            // or assign it to a new variable.
            // value types are deleted from memory as soon as the one variable that contains
            // them passes out of scope.

            // reference types, we get a new copy of a reference,
            // but to the same underlying object.
            // reference types need to be "garbage collected" because we don't know right away
            // when the LAST variable pointing to it has passed out of scope.

            // in C# we have the idea of "managed" vs "unmanaged" code -
            // in unmanaged code, you have to manually write the code to
            // delete reference type objects from memory, source of many bugs.

            // in managed code, there is garbage collection the runs periodically
            // to search for objects that are unreachable by any running part of the code.

            // our tradeoff is, the computer should work harder so the developer can solve
            // real problems

            // back to strings......

            // NORMALLY "==" compares value types by value, and reference types by reference.

            Console.WriteLine(new Dummy() == new Dummy()); // false... reference types.
            // for value types like structs, they don't have to be the same object
            // just have the same values.

            // BUT we make an exception for strings because it's awkward to have to
            // do string.Equals() for comparing strings.

            // in C#, all value types do derive ultimately from object.
            // so, we can always upcast them to object variables.

            int    i1 = 5;
            object o2 = i1; // implicit upcasting
            // this is called "boxing" - the int is wrapped inside a reference type
            // to give that value reference type semantics
            // "unboxing"... the reverse, with downcasting.
            int i2 = (int)o2; // extract that value from inside the object wrapper.

            i1 = 8;
            Console.WriteLine((int)o2); // should print 5

            // java has awkward Integer vs int distinctions
            // we have this as boxing and unboxing with object.

            // sometimes we want to see if two objects have the same values in them
            // for that, we make use of .Equals
        }
Example #3
0
        static void StringEquality()
        {
            string a = "asdf";
            string b = "asdf";

            Console.WriteLine(a == b); //returns true

            //value types and reference types:
            //  value type variables store their values directly
            //  reference type variables store a reference to their value

            //in C#, many of our basic types are value types
            //  int, double, float, bool, long,  etc.

            int n1 = 5;
            int n2 = n1; //int is value type, so n2 is a copy of n1

            n1 = 6;      //n2 stays at 5 because it's a value type

            var dummy1 = new Dummy();
            var dummy2 = dummy1;

            dummy1.data = 10;
            if (dummy2.data == 10)
            {
                Console.WriteLine("reference type");
            }
            else
            {
                Console.WriteLine("value type");
            }
            //Dummy is a reference type so dummy2 is a copy of the reference,
            //  i.e. a reference to the same object; changes to dummy1 will
            //  also change dummy2

            var dummy3 = new ValueDummy();
            var dummy4 = dummy3;

            dummy3.data = 10;
            if (dummy4.data == 10)
            {
                Console.WriteLine("reference type");
            }
            else
            {
                Console.WriteLine("value type");
            }
            //structs are copie entirely every time we pass it to a new method
            //  or assign it to a new variable
            //value types are deleted from memory as soon as the one variable
            //  that contains them passes out of scope

            //reference types we get a new copy of a reference but to the same
            //  underlying object
            //reference types need to be "garbage collected" because we don't
            //  know right away when the LAST variable pointing to it passed
            //  out of scope.

            //objects made from classes are always reference types
            //objects made from structs are value types
            //all the built-in value types are "structs" in C#

            //in C# we hav ethe idea of "managed" vs "unmanaged" code
            //in unmanaged code you have to manually write the code to
            //  delete reference from memory

            //in managed code, there is garbage collection that runs periodically
            //  to search for objects that are unreachable by any running part of the code

            //our tradeoff is, the computer should work harder so the deeloper can solve
            //  real problems

            //back to strings......

            //NORMALLY "==" compares value types by value and reference types by reference
            Console.WriteLine(new Dummy() == new Dummy()); //false... reference types

            //For value types like struct, they don't need to be the same object, only
            //  the same value

            //BUT we make an exception for strings because it's awkward to have to
            //  do string.Equals() for comparing strings.

            //In C# all value types do derive ultimately from object so we can always
            //  upcast them to object variables

            int    i1 = 5;
            object o1 = i1; //implicit upcasting going on
            //this is caleld "boxing" - the int is wrapped inside a reference type
            //  to give that value reference type semantics

            //Java has awkward Integer vs int distinctions
            //we have this as boxing and unboxing with object
            //"Unboxing" the reverse, with downcasting

            int i2 = (int)02; //extract that value from inside the object wrapper

            //sometimes we want to see if two objects have ethe same values in them
            //  for that we make use of a .Equals method
        }
Example #4
0
        static void StringEquality()
        {
            string a = "asdf";
            string b = "asdf";

            Console.WriteLine(a == b); //returns true

            //value types and reference types
            //value types vars store their values directly
            // ref type var stores a ref to their val

            // in c# many of our basic types are value types
            // int, double, bool, float, long

            int n1 = 5;
            int n2 = n1; // int is value type so n2 gets a copy of n1

            var dummy1 = new Dummy();
            var dummy2 = dummy1;

            dummy1.Data = 10;
            if (dummy2.Data == 10)
            {
                Console.WriteLine("reference type");
            }
            else
            {
                Console.WriteLine("Value type");
            }
            // Dummy is a ref type, so dummy2 is a copy of the ref
            // e.g. a new ref to the same object

            // obj made from classes are ref types, always
            // obj made from structs are value types
            // all the built in val types are structs in c#

            var vDummy1 = new ValueDummy();
            var vDummy2 = vDummy1;

            vDummy2.Data = 10;
            if (vDummy2.Data == 10)
            {
                Console.WriteLine("reference type");
            }
            else
            {
                Console.WriteLine("Value type");
            }

            //structs are copied entirely every time we pass it to a new method or assign to a new var
            // value types are deleted from mem as soon as it goes out of scope

            // ref types we get a new copy of ref
            // but to the same underlying obj
            // ref types needs to be garbage collected because we don't know right away
            // when the last var pointing to it has passed out of scope

            // in C# we have idea of managed vs unmanaged code
            // in unmanaged code you have to manually write the code to delete ref type obj from mem

            // in managed code, there is a garbage that runs periodically
            // to search for obj that are unreachable by any running parts of the code

            // tradeoff, computer should work harder so the dev can solve real problems

            // back to strings

            // Normally '==' compares value types by value, and ref by ref

            Console.WriteLine(new Dummy() == new Dummy()); //false... ref types
            // for val types like structs, they don't have to be the same obj
            // just have the same value

            // but we have an exception for strings because it's awkward to have to
            // do string.Equals() for comparing strings
            //"abc" == "abc";

            // in C#, all value types do derive ultimately from obj
            // so we can always upcast them to obj vars
            int    i1 = 5;
            object o2 = i1; //implicit upcasting, aka boxing
            // the int is wrapped inside a ref type
            // to give that value ref type semantics
            // "unboxing" ... the reverse with downcasting
            int i2 = (int)o2; // extract that valu from inside the obj wrapper

            // java has awkward Integer vs int distinctions
            // we have this as boxing and unboxing with object

            // sometimes we want to see if two objs have the same values in them
            // for that we make use of .Equals
        }