static void Main(string[] args)
        {
            // 用于演示集合操作的例子.
            CollectionSample collectionSample = new CollectionSample();

            // ArrayList
            collectionSample.ArrayListSample();

            // Hashtable
            collectionSample.HashtableSample();

            // Queue
            collectionSample.QueueSample();

            // SortedList
            collectionSample.SortedListSample();

            // Stack
            collectionSample.StackSample();



            GenericCollectionSample genericCollectionSample = new GenericCollectionSample();

            // List<T>
            genericCollectionSample.ListSample();

            // Dictionary <T1,T2>
            genericCollectionSample.DictionarySample();

            // Queue<T>
            genericCollectionSample.QueueSample();

            // Stack<T>
            genericCollectionSample.StackSample();

            Console.ReadLine();
        }
Exemple #2
0
        public void GetCustomer_ShouldThrowIndexOutOfRangeException()
        {
            // arrange
            Customer[] customers = new Customer[2]
            {
                new Customer {
                    FirstName = "jhon", LastName = "ford"
                },
                new Customer()
                {
                    FirstName = "jhon2", LastName = "ford2"
                }
            };

            int      index          = 3;
            Customer expectedResult = customers[1];

            // act
            Customer actualResult = CollectionSample.GetCustomer(customers, index);

            // assert
            Assert.AreEqual(expectedResult, actual: actualResult);
        }
Exemple #3
0
        public void GetCustomer_ShouldGetSameCustomer()
        {
            // arrange
            Customer[] customers = new Customer[2]
            {
                new Customer {
                    FirstName = "jhon", LastName = "ford"
                },
                new Customer()
                {
                    FirstName = "jhon2", LastName = "ford2"
                }
            };

            int      index          = 1;
            Customer expectedResult = customers[index];

            // act
            Customer actualResult = CollectionSample.GetCustomer(customers, index);

            // assert
            Assert.IsInstanceOfType(actualResult, typeof(Customer));
            Assert.AreEqual(expectedResult, actual: actualResult);
        }
Exemple #4
0
        static void Main(string[] args)
        {
            //Collections sample

            CollectionSample cse = new CollectionSample();

            Console.WriteLine(cse.Factorial(5) + "\t" + cse.FactorialRecursion(5));
            Dictionary <int, string> dic = cse.GetNameAndNumbers();

            foreach (int key in dic.Keys)
            {
                Console.WriteLine(dic[key]);
            }
            Console.WriteLine("Please enter the number till which you want prime numbers");

            List <int> pn = cse.GetPrimeNumbers(Convert.ToInt32(Console.ReadLine()));

            foreach (int primeNumber in pn)
            {
                Console.WriteLine(primeNumber);
            }

            //Singleton sample
            SingletonSample sgs  = SingletonSample.CreateSingletonObject();
            SingletonSample sgs1 = SingletonSample.CreateSingletonObject();
            FileSamples     fs1  = new FileSamples();
            FileSamples     fs2  = new FileSamples();

            Console.WriteLine(sgs.GetHashCode() + "\t" + sgs1.GetHashCode());
            Console.WriteLine(fs1.GetHashCode() + "\t" + fs2.GetHashCode());
            //File Samples
            FileSamples fs = new FileSamples();

            fs.WriteTextToFile("C:\\Users\\v-mukri\\Desktop\\SampleFiles\\1.txt", "Hello");
            fs.AppendToFile("C:\\Users\\v-mukri\\Desktop\\SampleFiles\\1.txt", "\nAppending");
            string[] fileLines   = fs.ReadAllLines("C:\\Users\\v-mukri\\Desktop\\SampleFiles\\1.txt");
            string   fileContent = fs.ReadAllText("C:\\Users\\v-mukri\\Desktop\\SampleFiles\\1.txt");

            try
            {
                File.Copy("C:\\Users\\v-mukri\\Desktop\\SampleFiles\\1.txt", "C:\\Users\\v-mukri\\Desktop\\SampleFiles\\2.txt");
            }
            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.GetType());
                if (e.Message.Contains("already exists"))
                {
                    Console.WriteLine("Please choose a different file name");
                }
                try
                {
                    throw new ArithmeticException();
                }
                catch
                {
                    Console.WriteLine("We have never dealt with this kind of exception, we are investigating into it, Arithmetic");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("We have never dealt with this kind of exception, we are investigating into it");
            }
            finally
            {
                Console.WriteLine("Mandatory code to be executed");
            }
            File.Delete("C:\\Users\\v-mukri\\Desktop\\SampleFiles\\1.txt");
            File.Delete("C:\\Users\\v-mukri\\Desktop\\SampleFiles\\1.txt");
            foreach (string samplestring in fileLines)
            {
                Console.WriteLine(samplestring);
            }
            Console.WriteLine(fileContent);

            Program p = new Program();

            p.BaseClassMethod();

            //Classes and Object
            Fruit mango = new Fruit();

            Console.WriteLine("Name:" + mango.name + "\tColor:" + mango.color + "\tTaste:" + mango.taste);
            mango.name  = "Mango";
            mango.color = "Yello";
            mango.taste = "sweet";
            Console.WriteLine("Name:" + mango.name + "\tColor:" + mango.color + "\tTaste:" + mango.taste);
            Fruit orange = new Fruit("Orange", "Soar", "Orange");

            Console.WriteLine("Name:" + orange.name + "\tColor:" + orange.color + "\tTaste:" + orange.taste);

            //Access specifiers
            PPPInheritance      ppp = new PPPInheritance();
            PrivateAndProtected pnp = new PrivateAndProtected();
            int a = pnp.publicVariabel;

            //Method overloading
            MethodOverLoading mol = new MethodOverLoading();

            mol.Add(1, 2);
            mol.Add(1, 2.3);

            //Method overriding
            MORSubClass mor = new MORSubClass();

            mor.SuperClassMethod();

            //Variables sample
            VariableSamples vs = new VariableSamples();

            vs.SampleMethod();

            //Input reading
            InputReading ir = new InputReading();

            ir.ReadInteger();

            //Operators sample
            OperatorsSample os = new OperatorsSample();

            os.Modulus(4, 3);
            a  = 10;
            a %= 5;
            Console.WriteLine(a);
            Console.WriteLine(os.IfAGreaterThanB(1, 6));
            Console.WriteLine(os.IfAEqualsB(1, 1));

            int b = (1 == 2) ? 2 : 1;

            Console.WriteLine(b);
            b = (1 == 1) ? 2 : 1;
            Console.WriteLine(b);

            Console.WriteLine(Math.Floor(12.1));
            Console.WriteLine(Math.Ceiling(12.1));

            //String samples
            StringSamples ss        = new StringSamples();
            string        firstName = "John";
            string        lastName  = "Doe";
            string        name      = $"My full name is: {firstName} {lastName}";

            Console.WriteLine(name);

            //Condition samples
            ConditionSample cs = new ConditionSample();

            cs.Conditions();

            //Loop samples
            LoopSamples ls = new LoopSamples();

            ls.Loop();

            //Statci variable samples
            Console.WriteLine("Static variable value is " + StcObjVariables.a);
            StcObjVariables stcObj = new StcObjVariables(), stcObj1 = new StcObjVariables();

            Console.WriteLine("Object1 variable value is " + stcObj.b);
            Console.WriteLine("Object2 variable value is " + stcObj1.b);
            stcObj.b = 10;
            Console.WriteLine("Object1 variable value is " + stcObj.b);
            Console.WriteLine("Object2 variable value is " + stcObj1.b);
            stcObj.ContinueAndBreak();



            //ArraySamples
            new ArraySamples().ArraySamplesMethod();

            //Method Samples
            MethodSamples ms  = new MethodSamples();
            MethodSamples ms1 = new MethodSamples(2);
            MethodSamples ms2 = new MethodSamples(1, 2);
            int           a1  = ms2.SimpleIntMethod();
            string        b1  = ms2.SimpleStringMethod("a");
            int           v   = MethodSamples.SimpleAdd(1, 3);

            ms2.MyMethod();
            ms2.MyMethod("India");
        }