static void Main(string[] args)
        {
            //  Passing ref-types by value.
            Console.WriteLine("***** Passing Person object by value *****");
            Person fred = new Person("Fred", 12);
            Console.WriteLine("]nBefore by value call, Person is:");
            fred.Display();

            SendAPersonByValue(fred);
            Console.WriteLine("\nAfter by value call, Person is:");
            fred.Display();

            //  Passing ref-types by reference.
            Console.WriteLine("\n***** Passing Person object by reference *****");

            Person mel = new Person("Mel", 23);
            Console.WriteLine("Before by ref call, Person is:");
            mel.Display();

            SendAPersonByReference(ref mel);
            Console.WriteLine("After by ref call, Person is:");
            mel.Display();

            Console.ReadKey();
        }
示例#2
0
        static void Main(string[] args)
        {
            // Passing ref-types by ref.
            Console.WriteLine("***** Passing Person object by reference *****\n");
            Person mel = new Person("Mel", 23);

            Console.WriteLine("Before by ref call, Person is:");
            mel.Display();

            SendAPersonByValue(mel);
            Console.WriteLine("After value call.");
            mel.Display();

            SendAPersonByReference(ref mel);
            Console.WriteLine("After by ref call, Person is:");
            mel.Display();
            Console.ReadLine();
        }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Passing a class by value");
            Person fred = new Person("Fred", 12);
            Console.WriteLine("Before pass by val: ");
            fred.Display();

            SendAClassByValue(fred);

            Console.WriteLine("After pass by val");
            fred.Display();

            SendAClassByReference(ref fred);
            Console.WriteLine("After pass by ref");
            fred.Display();

            Console.ReadLine();

            //Person p = new Person();
        }
示例#4
0
        static void Main(string[] args)
        {
            // Передача ссылочных типов по значению.
            Console.WriteLine("***** Passing Person object by value *****");
            Person fred = new Person("Fred", 12);

            Console.WriteLine("\nBefore by value call, Person is:"); // перед вызовом
            fred.Display();
            SendAPersonByValue(fred);
            Console.WriteLine("\nAfter by value call, Person is:"); // после вызова
            fred.Display();

            Console.WriteLine("***** Passing Person object by value 2 *****");
            Person mel = new Person("Mel", 23);

            Console.WriteLine("Before by ref call, Person is:"); // перед вызовом
            mel.Display();
            SendAPersonByReference(ref mel);
            Console.WriteLine("After by ref call, Person is:"); // после вызова
            mel.Display();
            Console.ReadLine();
        }
示例#5
0
        static void Main(string[] args)
        {
            // Passing ref-types by value.
            Console.WriteLine("***** Passing Person object by value *****");
            Person fred = new Person("Fred", 12);

            Console.WriteLine("\nBefore by value call, Person is:");
            fred.Display();
            SendAPersonByValue(fred);
            Console.WriteLine("\nAfter by value call, Person is:");
            fred.Display();

            Person mel = new Person("Mel", 23);

            Console.WriteLine("Before by ref call, Person is:");
            mel.Display();
            SendAPersonByReference(ref mel);
            Console.WriteLine("After by ref call, Person is:");
            mel.Display();

            Console.ReadLine();
        }
示例#6
0
        static void Main(string[] args)
        {
            //Console.WriteLine("**** Refernce and value types in params ****");
            //Console.WriteLine("=> Passing person by value");
            //Person fred = new Person("Fred", 12);
            //Console.WriteLine("Before by val call person is:");
            //fred.Display();
            //SendAPersonByValue(fred);
            //Console.WriteLine("After by val call person is:");
            //fred.Display();

            Console.WriteLine("=> Using out parameter:");
            int i;

            SendUsingOut(out i); // Must be assigned in called method

            Console.WriteLine("=> Using ref parameter:");
            int j;

            j = 1;
            SendUsingRef(ref j); // int must be assigned befor using

            Console.WriteLine("=> Passing person by ref");
            Person nick = new Person("Nick", 76);

            Console.WriteLine("Before by vref call person is:");
            nick.Display();
            SendAPersonByReference(ref nick); // nick must be assigned befor using
            Console.WriteLine("After by ref call person is:");
            nick.Display();

            Console.WriteLine("=> Passing person with out parameter:");
            Person jack;

            SendAPersonByOut(out jack); // jack must be assigned in called method
            jack.Display();
            Console.ReadLine();
        }
示例#7
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Passing Person Object By Value ****");
            Person fred = new Person("Fred", 12);

            Console.WriteLine("\nBefore by value call, Person is:");
            fred.Display();

            SemdAPersonByValue(fred);
            Console.WriteLine("\nAfter call by value, Person is:");
            fred.Display();

            Console.WriteLine("**** Passing Person object By referece ****");
            Person mel = new Person("Mel", 18);

            Console.WriteLine("Before any call person is:");
            mel.Display();

            SendAPersonByReference(ref mel);
            Console.WriteLine("After a call by ref the person is:");
            mel.Display();

            Console.ReadLine();
        }
示例#8
0
        public static void Main(string[] args)
        {
            //Passing ref-types by Value
            Console.WriteLine("*****Passing Person Object by value*****");
            Person fred = new Person("Fred", 34);

            Console.WriteLine("\n Before by value call, Person is");
            fred.Display();

            SendAPersonByValue(fred);
            Console.WriteLine("\n After by value call, Person is");
            fred.Display();

            //Passing ref-types by ref

            Console.WriteLine("****** Passing Person Object by Reference *****");

            Person mel = new Person("Melvin", 23);

            Console.WriteLine("Before by ref call, Person is");
            mel.Display();

            /** Returned the whole new Person Object ***/
            SendAPersonByReference(ref mel);
            Console.WriteLine("After by ref call, Person is");
            mel.Display();

            /* Changed only the value of age property of the Person Object*/
            // Passing ref types by value
            SendAPersonByValue(mel);
            mel.Display();


            // WORKING WITH DATABASEREADER CLASS
            Console.WriteLine("*** Fun with Nullable Vlaue Types **** \n");
            DatabaseReader dr = new DatabaseReader();

            //If the value from GetIntFromDatabase is null
            // assign local variable to 100

            int myData = dr.GetIntFromDatabase() ?? 100;

            Console.WriteLine("Value of myData: {0}", myData);

            //Get int from 'database'
            int?i = dr.GetIntFromDatabase();

            if (i.HasValue)
            {
                Console.WriteLine("Value of i is {0}", i.Value);
            }
            else
            {
                Console.WriteLine(" Value of i is undefined");
            }


            //Get Bool from 'database'

            bool?b = dr.GetBooleanFromDatabase();

            if (b != null)
            {
                Console.WriteLine("Value of b is {0}", b.Value);
            }
            else
            {
                Console.WriteLine(" Value of b is undefined");
            }

            //Null Coealesicng Assignment Operation( c#8.0)

            /* This operator assigns the left hand side to the righthand side only if the
             * the left hand side is null
             */

            int?nullableInt = null;

            nullableInt ??= 24; // assigns nullableInt the value of 24 as is null
            nullableInt ??= 14; // does not assign as nullableInt already has a value of 24
            Console.WriteLine(nullableInt);
            string? [] nullableString = { "ASD", "ASDF" };
            TesterMethod(args: nullableString);


            //Tuples => Getting starting with Tuples

            (string, int, string)values = ("a", 2, "b");
            // also follwing is valid
            var values2 = ("x", 2, "z");

            // Accessing  tuple elements
            Console.WriteLine($" First Item: {values.Item1}");
            Console.WriteLine($" First Item: {values.Item2}");
            Console.WriteLine($" First Item: {values.Item3}");


            // Name values tuple
            (string firstName, string lastName, int age)info = ("Hari", "Mahat", 34);

            // can be accesed by ItemX= Item position, 1 in above is firstName
            Console.WriteLine($" First Name: {info.Item1}");

            // accing using name
            Console.WriteLine($" First Item: {info.lastName}");

            // values with Names
            var info2 = (firstName : "Hari", lastName : "Mahat", age : 23);


            // Calling Tuple as Methods Return values

            var samples = FillTheseValues2();

            Console.WriteLine($" Int is {samples.a}");
            Console.WriteLine($" String is {samples.b}");
            Console.WriteLine($" Boolean is {samples.c}");

            // Destructed Tuple allows singple method call to get the individaul values of the
            // structre by returning a tuple

            Point p           = new Point(-5, 6);
            var   pointValues = p.Deconstruct();

            Console.WriteLine($" X is : {pointValues.xPos}");
            Console.WriteLine($" Y is : {pointValues.yPos}");

            // Using tuple value to get quadrant

            Console.WriteLine(GetQuadtrant1(p));
        }