Beispiel #1
0
        public static void objectsForOtherClassesInSameNameSpace()
        {
            Console.WriteLine("objectsForOtherClassesInSameNameSpace Method: ");
            Console.WriteLine("Uses Introduce method in Person class- ");
            //CREATE OBJECT FOR PERSON
            Person john = new Person()
            {
                Age = 20
            };

            john.FirstName = "John";
            john.LastName  = "Smith";
            john.Introduce();
            Console.WriteLine("John is currently {0} years old", john.Age);
            //calling incrementAge and MakeOld methods in Person class
            john.IncrementAge(john.Age);
            john.MakeOld(john);

            Console.WriteLine();

            Console.WriteLine("Uses Add method in Calculator class located in another folder of HelloWorld namespace- ");
            Calculator calculator = new Calculator();
            int        result     = calculator.Add(1, 2);

            Console.WriteLine(result);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var john = new Person();

            john.FirstName = "John";
            john.LastName  = "Smith";
            john.Introduce();

            Calculator calculator = new Calculator();
            var        result     = calculator.Add(1, 2);

            Console.WriteLine(result);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            // Declaring new object with Person class
            var john = new Person();

            // Using dot notation
            john.FirstName = "John";
            john.LastName  = "Smith";

            // Calling method on john object
            john.Introduce();

            Calculator calculator = new Calculator();
            var        result     = calculator.Add(1, 2);

            Console.WriteLine(result);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            Person John = new Person();

            John.FirstName = "John";
            John.LastName  = "Smith";
            John.Introduce();

            Calculator myCal = new Calculator();

            Console.WriteLine(myCal.Add(1, 6));

            var method = ShippingMethod.Express;

            Console.WriteLine((int)method);

            var methodId = 3;

            Console.WriteLine((ShippingMethod)methodId);

            Console.WriteLine(method.ToString()); //This even brings back Express

            //Convert a string to an Enum:
            var methodName     = "Express";
            var shippingMethod = (ShippingMethod)Enum.Parse(typeof(ShippingMethod), methodName);

            switch (shippingMethod)
            {
            case ShippingMethod.Express:
                Console.WriteLine("Test");
                break;

            default:
                break;
            }

            //Exercise1 c = new Exercise1();
            //c.demo();
            //Exercise3 c = new Exercise3();
            //c.demo();
            Exercises4 c = new Exercises4();

            c.demo();
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            /*
             * Type Conversions
             */
            byte b = 1;
            int  i = b; // Implicit Type conversion

            Console.WriteLine(i);

            int  a = 5;
            byte c = (byte)a; // Explicit Type Conversion

            Console.WriteLine(c);

            int  d = 500;
            byte e = (byte)d;

            Console.WriteLine(e); // Outputs 244 since it is the maximum value

            var number = "1234";
            int f      = Convert.ToInt32(number);

            Console.WriteLine(f);

            string fasak = "debba";

            fasak = "abba";
            Console.WriteLine(fasak);

            try
            {
                var g = "1234";
                int h = Convert.ToByte(g);
                Console.WriteLine(h);
            }
            catch
            {
                Console.WriteLine("Cannot convert");
            };

            var obj = new Person();

            obj.firstName = "John";
            obj.lastName  = "doe";
            obj.Introduce();

            Calculator calculate = new Calculator();

            Console.WriteLine(calculate.Add(5, 2));

            // Non Primitive data types
            var numbers = new int[3];

            numbers[0] = 1;
            Console.WriteLine(numbers[0]);
            Console.WriteLine(numbers[1]); // No value is initialized Outputs 0 because it is the default value of integer datatype
            Console.WriteLine(numbers[2]); // Outputs 0


            var flags = new Boolean[3];

            flags[0] = true;
            Console.WriteLine(flags[0]);
            Console.WriteLine(flags[1]); // When no values is initialized it will Output False since it is the default value

            var names = new string[3] {
                "Praveen", "kumar", "guda"
            };                                                        // We can also intiliase in this way

            // Escape characters

            /**
             *  \n      New Line
             *  \t      Tab
             *  \\      Backslash
             *  \'      Single Quotation Mark
             *  \"      Double Quotation Mark
             */

            var firstName = "Praveen Kumar";
            var lastName  = "Guda";

            string fullName = firstName + " " + lastName;

            var alternateway = string.Format("My name is {0} {1}", firstName, lastName);

            var names1 = new string[3] {
                "john", "jack", "Mary"
            };
            var formatedNames = string.Join(",", names1);

            Console.WriteLine(formatedNames);

            var text = "Hi John\nLook into the following paths\nc:\\folder1\\folder2\\folder3";

            Console.WriteLine(text);

            // Verbatim string
            var verbatimString = @"Hi John
Look into the following paths
c:\folder1\folder2\folder3";

            Console.WriteLine(verbatimString);

            // Using Shippingmethod enum
            var method = ShippingMethod.Express;

            Console.WriteLine((int)method); // Converting enum to an interger by casting

            var methodId = 3;

            Console.WriteLine((ShippingMethod)methodId); // Converting interger to an enum by casting

            Console.WriteLine(method.ToString());

            var methodName        = "Express";
            var shippingMethodval = (ShippingMethod)Enum.Parse(typeof(ShippingMethod), methodName); // Converting string to an enum by casting

            // There are two main types in c#
            // Structures       Primitive types (int, char, float, bool ...), Custom structures ----> Value Types
            // Classes          Arrays, Strings, Custom classes -------> Reference Type
            // Value types and Reference types

            // value types are primitive datatypes ----> Stored in Stack
            var j = 10;
            var k = j;

            k++;
            Console.WriteLine(string.Format("j : {0}, k : {1}", j, k)); // Outputs j : 10, k : 11 because values are copied

            // Reference types are non-primitive datatypes -----> Stored in Heap
            var array1 = new int[3] {
                1, 2, 3
            };
            var array2 = array1; // Memory reference is copied not actuall value

            array2[0] = 55;
            Console.WriteLine(string.Format("array1 : {0}, array2 : {1}", array1[0], array2[0]));
            // The value of first element will be changed for both the arrays because they are non primitive types which are referenced.


            var debbaNumber = 1;

            Increment(debbaNumber);
            Console.WriteLine(debbaNumber); // Outputs 1 because here since it is value type so a copy will be sent

            var person = new Person1()
            {
                Age = 20
            };

            MakeOld(person);
            Console.WriteLine(person.Age); // outputs 30 becuase it is reference type and value is accessed from the memory


            // Enums
            var season = Season.Autumn;

            switch (season)
            {
            // One outcome for both the conditions
            case Season.Autumn:
            case Season.Summer:
                Console.WriteLine("It is summer and autumn");
                break;

            case Season.Winter:
                Console.WriteLine("It is winter");
                break;

            default:
                Console.WriteLine("I dont know");
                break;
            }

            // Iteration Systems- ------------------------------------------------------
            // For, Foeach, While and Do-while
            // Forloop, normal as we do
            for (var temp = 0; temp <= 10; temp++)
            {
                if (temp % 2 == 0)
                {
                    Console.WriteLine(temp);
                }
            }
            // Foreach is only for inumerable like arrays, and strings
            var randomName = "John Doe";

            foreach (var character in randomName)
            {
                Console.WriteLine(character);
            }

            var randomNumbers = new int[] { 1, 2, 3, 4, 5 };

            foreach (var num in randomNumbers)
            {
                Console.WriteLine(num);
            }

            // While loop use it when we dont know the number of iterations
            // break causes any loop to terminate
            // continue causes any loop to execute next iteration
            while (true)
            {
                Console.WriteLine("Type your name:");
                var input = Console.ReadLine();

                if (!String.IsNullOrWhiteSpace(input))
                {
                    Console.WriteLine("My name is :" + input);
                    continue;
                }
                break;
            }

            // Random Numbers ---------------------------------------
            var       random         = new Random();
            const int passwordLength = 10;
            var       buffer         = new char[passwordLength];

            for (var dd = 0; dd < passwordLength; dd++)
            {
                buffer[i] = (char)('a' + random.Next(0, 26)); // didnt worked but need to see
            }
            Console.WriteLine(buffer);
            var password = new string(buffer);

            Console.Write(password);


            // Arrays have fixed size---------------------------------
            // Jagged Array ->
            var jaggedArray = new int[3][];

            jaggedArray[0] = new int[] { 1, 2, 3, 4 };
            jaggedArray[1] = new int[] { 1, 2 };
            jaggedArray[2] = new int[] { 2, 5, 8 };



            var numbersArray = new[] { 2, 5, 6, 4, 8 }; //Initializing array with object initialization syntax;

            // Length - returns the length of the array
            Console.WriteLine("Length: " + numbersArray.Length);

            // IndexOf() - returns index of the element
            var indexresult = Array.IndexOf(numbersArray, 5);

            Console.WriteLine("index = " + indexresult);

            // Clear() - clear the items from starting index and no of elements to be deleted
            // Clearing the items in array of objects means making them null
            // Clearing the items in array of booleans means making them false
            Array.Clear(numbersArray, 0, 2);
            Console.WriteLine("Elemtns in array after clearing");
            foreach (var item in numbersArray)
            {
                Console.WriteLine(item);
            }

            // Copy() - copies elements from one array to another
            var anotherArray = new int[3];

            Array.Copy(numbersArray, anotherArray, 3);
            Console.WriteLine("Array after copying");
            foreach (var copiedItem in anotherArray)
            {
                Console.WriteLine(copiedItem);
            }

            // Sort() - sort elements in the array
            Array.Sort(numbersArray);
            Console.WriteLine("Elements after sorting");
            foreach (var sortedelements in numbersArray)
            {
                Console.WriteLine(sortedelements);
            }

            // Reverse()
            Array.Reverse(numbersArray);
            Console.WriteLine("Elements after Reverse");
            foreach (var revElement in numbersArray)
            {
                Console.WriteLine(revElement);
            }

            // Lists dynamic size -----------------------------------

            var numberList = new List <int>()
            {
                1, 2, 3, 4, 5, 6, 5
            };                 // Initializing list with object initialization syntax

            numberList.Add(7); // Add one element to the list just like push
            numberList.AddRange(new int[3] {
                8, 9, 10
            });                                           // Add more than one element

            foreach (var listItem in numberList)
            {
                Console.WriteLine(listItem);
            }

            Console.WriteLine("Index of 5 " + numberList.IndexOf(5));
            Console.WriteLine("Last Index of 5 " + numberList.LastIndexOf(5));
            Console.WriteLine("Count " + numberList.Count);

            numberList.Remove(8);
            Console.WriteLine("List after removing  8");

            foreach (var listItem in numberList)
            {
                Console.WriteLine(listItem);
            }

            for (var adf = 0; adf < numberList.Count; adf++)
            {
                if (numberList[adf] == 1)
                {
                    numberList.Remove(numbers[adf]);
                }
            }

            numberList.Clear();

            Console.WriteLine("List count after clearing = " + numberList.Count);

            // Date  -------------------------------------------
            var dateTime        = new DateTime(2015, 1, 25);
            var currentDateTime = DateTime.Now;
            var todaysDate      = DateTime.Today;

            Console.WriteLine("Hour: " + currentDateTime.Hour);
            Console.WriteLine("Minute: " + currentDateTime.Minute);

            // to change the datetime object
            var tomorrow  = currentDateTime.AddDays(1);
            var yesterday = currentDateTime.AddDays(-1);

            Console.WriteLine("Long Date string = " + currentDateTime.ToLongDateString());
            Console.WriteLine("Short Date string = " + currentDateTime.ToShortDateString());
            Console.WriteLine("Long Time string = " + currentDateTime.ToLongTimeString());
            Console.WriteLine("Short Time string = " + currentDateTime.ToShortTimeString());
            Console.WriteLine("Date and Time = " + currentDateTime.ToString());
            Console.WriteLine("Date and Time = " + currentDateTime.ToString("yyyy-MM-dd"));

            // Time --------------------------------------------
            // Creating timespans
            var timeSpan  = new TimeSpan(1, 2, 3); //(Hour, Minutes, Seconds)
            var timeSpan1 = new TimeSpan(1, 0, 0);
            var timeSpan2 = TimeSpan.FromHours(1);

            var start    = DateTime.Now;
            var end      = DateTime.Now.AddMinutes(2);
            var duration = end - start;

            Console.WriteLine("Duration : " + duration);

            // Accessing properties
            Console.WriteLine("Minutes = " + timeSpan.Minutes);
            Console.WriteLine("total Minutes = " + timeSpan.TotalMinutes);

            // Add
            Console.WriteLine("Add time  = " + timeSpan.Add(TimeSpan.FromMinutes(8)));
            Console.WriteLine("Subtract time  = " + timeSpan.Subtract(TimeSpan.FromMinutes(8)));

            // ToString
            Console.WriteLine("ToString = " + timeSpan.ToString());
            // Parse
            Console.WriteLine("Parse = " + TimeSpan.Parse("01:02:03"));
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            byte   number     = 2;
            int    count      = 10;
            float  totalPrice = 20.95f;
            bool   isTired    = false;
            char   character  = 'A';
            string name       = "Asif";

            Console.WriteLine(number);
            Console.WriteLine(count);
            Console.WriteLine(totalPrice);
            Console.WriteLine(isTired);
            Console.WriteLine(character);
            Console.WriteLine(name);
            Console.WriteLine("{0} {1}", byte.MaxValue, 'B');

            //try
            //{
            //    string sales = "1234";
            //    byte b = Convert.ToByte(sales);
            //    Console.WriteLine(b);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Error Found");
            //}

            var z = 10;
            var y = 3;

            Console.WriteLine(z / y);
            Console.WriteLine((float)z / (float)y);

            byte number1 = 255;

            number1 += 2;

            Console.WriteLine(number1);
            var person = new Person
            {
                FirstName = "Asif",
                LastName  = "Khan"
            };

            person.Introduce();

            var numArray = new char[3] {
                'a', 'b', 'c'
            };

            Console.WriteLine(numArray[1]);

            //var input = new ChackNumberValid();
            //input.validity();
            //input.findMax();
            //input.checkDimension();

            var input = new speedLimitCheck();

            input.checkLimit();
        }
        static void Main(string[] args)
        {
            //var number = 0;
            //var count = 10;
            //var totalPrice = 20.99f;
            //double total = 30.45;
            //var character = 'a';
            //string example = "string";
            //bool isTrue = false;
            //Console.WriteLine(number);
            //Console.WriteLine(totalPrice);
            //Console.WriteLine(character);
            //Console.WriteLine(example);

            //Console.WriteLine("{0} {1}", byte.MinValue, byte.MaxValue);

            //int i = 1;
            //byte b = (byte)i;

            //Console.WriteLine(b);

            //var bumber = "1234";
            //int i = Convert.ToInt32(bumber);

            //try
            //{
            //    var num = "1234";
            //    int b = Convert.ToByte(num);
            //}
            //catch (Exception)
            //{

            //    Console.WriteLine("Number cannot be converted");
            //}

            var john = new Person();

            john.firstName = "John";
            john.lastName  = "Smith";

            john.Introduce();

            Calculator c = new Calculator();

            c.Add(2, 3);

            //var number = new int[3];

            //number[0] = 1;
            //Console.WriteLine(number[0]);
            //Console.WriteLine(number[1]);
            //Console.WriteLine(number[2]);

            //var number = new string[3];

            //number[0] = "test";
            //Console.WriteLine(number[0]);
            //Console.WriteLine(number[1]);
            //Console.WriteLine(number[2]);

            var fn = "Joshua";
            var ln = "Fernandes";

            var name = new string[2] {
                fn, ln
            };

            Console.WriteLine(string.Format("My Name is {0} {1}", fn, ln));
            Console.WriteLine(string.Join(",", name));

            Console.WriteLine("==========ENUM===========");
            Console.WriteLine("Enum", EnumExample.example1, EnumExample.example2, EnumExample.example3);
            var method = EnumExample.example3;

            Console.WriteLine((int)method);

            var methodID = 3;

            Console.WriteLine((EnumExample)methodID);
            Console.WriteLine(EnumExample.example3.ToString());

            //Parsing
            var newMethod = "example1";
            var newEnum   = (EnumExample)Enum.Parse(typeof(EnumExample), newMethod);

            Console.WriteLine(newEnum);

            Console.WriteLine("==========================");

            Console.WriteLine("\n\n");

            Console.WriteLine("============Ref and Value Types==============");
            var a = 10;
            var b = a;

            ++a;

            Console.WriteLine("a:{0},b:{1}", a, b);

            var array1 = new int[3] {
                1, 2, 3
            };
            var array2 = array1;

            array2[0] = 0;

            Console.WriteLine("arr1[0]={0} , arr2[0]={1}", array1[0], array2[0]);

            Console.WriteLine("=============================================");

            Console.WriteLine("===================CONDITIONS===================");
            var cond = new Conditions();

            cond.GetDay();
            cond.GetPrice();
            cond.GetSeason(Conditions.Season.Winter);

            Console.WriteLine("===================EXERCISE 1===================");
            var ex1 = new IsValidNumber();

            ex1.CheckNumber();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var person = new Person();

            person.FirstName  = "Mistu";
            person.SecondName = "Pratikbhai Prajapati";
            person.Introduce();

            Calculator calculator = new Calculator();
            int        result     = calculator.Add(2, 2);

            Console.WriteLine("Add method Result is: " + result);

            // int[] Numbers = new int[3];
            var Numbers = new int[3];

            Numbers[0] = 10;
            Console.WriteLine(Numbers[0]);
            Console.WriteLine(Numbers[1]);
            Console.WriteLine(Numbers[2]);

            var Flags = new bool[3];

            Flags[0] = true;
            Console.WriteLine(Flags[0]);
            Console.WriteLine(Flags[1]);
            Console.WriteLine(Flags[2]);

            string path = @"c:\projects\sandeep\files"; // verbatim string

            Console.WriteLine(path);


            var method = ShippingMethod.Express;

            Console.WriteLine((int)method);

            var methodId = 3;

            Console.WriteLine((ShippingMethod)methodId);
            Console.WriteLine(method.ToString());

            // we have string -> enum

            var methodName = "Express";
            // parsing
            var shippingmethod = (ShippingMethod)Enum.Parse(typeof(ShippingMethod), methodName);

            Console.WriteLine(shippingmethod);

            //copying value type and reference type
            var a = 10;         // integers are value type
            var b = a;

            b++;
            Console.WriteLine(string.Format("a: {0} & b: {1}", a, b));

            var array1 = new int[] { 1, 2, 3 }; // array are object in C#, hence they are called reference type
            var array2 = array1;

            array2[0] = 0;
            Console.WriteLine("array1[0]: {0} & array2[0]: {1}", array1[0], array2[0]);

            int number = 1;

            Increment(number);
            Console.WriteLine(number);

            var person1 = new Person()
            {
                Age = 30
            };

            Console.WriteLine(person1.Age);
            AgeNumber(person1);
            Console.WriteLine(person1.Age);

            Console.Read();
        }