示例#1
0
        static void Main(string[] args)
        {
            #region Process : Start and Stop
            Process.Start("Notepad.exe");
            //Process.Start(@"C:\");
            //Note You can find a process name from task manager
            //Process.Start("chrome.exe");
            //Process.Start("https://www.redit.com");

            Process[] notepads = Process.GetProcessesByName("notepad");//closes all notepads
            foreach (var process in notepads)
            {
                process.Kill();
            }
            #endregion

            #region Multidimensional Array
            string[,] Array2D = new string[3, 2];

            Array2D[0, 0] = "Item 00";
            Array2D[0, 1] = "Item 01";

            Array2D[1, 0] = "Item 10";
            Array2D[1, 1] = "Item 11";

            Array2D[2, 0] = "Item 20";
            Array2D[2, 1] = "Item 21";
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("Array Row " + i);
                for (int j = 0; j < 2; j++)
                {
                    Console.WriteLine("Array Col " + j);
                    Console.WriteLine("ARRAY: " + Array2D[i, j]);
                }
            }
            #endregion

            #region Jagged Array
            //array of an array

            int[][] jaggedArrayOne = new int[3][];
            jaggedArrayOne[0] = new int[5] {
                0, 1, 2, 3, 4
            };
            jaggedArrayOne[1] = new int[2] {
                6, 7
            };
            jaggedArrayOne[2] = new int[3] {
                9, 8, 5
            };

            int[][] jaggedArrayTwo =
            {
                new int[] {  0,  1,  2,  3 },
                new int[] { 10, 11, 12, 13 },
                new int[] { 20, 21, 22, 23 },
                new int[] { 30, 31, 32, 33 },
            };

            Console.WriteLine("jaggedArrayTwo[1][4] : " + jaggedArrayTwo[1][3]);
            for (int i = 0; i < jaggedArrayTwo.Length; i++)
            {
                Console.WriteLine("accesing " + i);
                for (int j = 0; j < jaggedArrayTwo[i].Length; j++)
                {
                    Console.WriteLine("accesing " + j);
                    Console.WriteLine($"jaggedArrayTwo[{i}][{j}] : {jaggedArrayTwo[i][j]}");
                }
            }
            Console.WriteLine();
            #endregion

            #region Static Directives
            Console.WriteLine(Math.Round(2.5465, 2));
            WriteLine(Math.Round(2.5465, 2)); //using static System.Console;
            #endregion

            #region IsNullEmpty
            string naam = " ";
            //if(naam.Trim()=="" || naam is null)
            //{
            //    Console.WriteLine("EMPTY");
            //}
            //else
            //{
            //    Console.WriteLine("NOT EMPTY");
            //}
            if (string.IsNullOrEmpty(naam) || string.IsNullOrWhiteSpace(naam))
            {
                Console.WriteLine("EMPTY");
            }
            else
            {
                Console.WriteLine("NOT EMPTY");
            }
            #endregion

            #region Destructors
            Animals dog  = new Animals();
            var     name = Console.ReadLine();
            #endregion

            #region Assembly
            Assembly     assemObject = Assembly.GetEntryAssembly();
            AssemblyName assemName   = assemObject.GetName();

            Console.WriteLine(assemName.FullName);

            Console.WriteLine(assemName.CultureName);
            CultureInfo cultureInfo = new CultureInfo("en-GB");
            assemName.CultureInfo = cultureInfo;
            Console.WriteLine(assemName.CultureInfo.DisplayName);
            Console.WriteLine(assemName.CultureInfo.NativeName);
            Console.WriteLine(assemName.CultureInfo.NumberFormat.CurrencySymbol);
            Console.WriteLine(assemName.CultureInfo.Calendar);

            Console.WriteLine(assemName.Version);
            assemName.Version = new Version(2, 0, 0, 0);
            Console.WriteLine(assemName.Version);
            #endregion

            #region Attributes : Default
            Persons.SayHi();
            Persons.SayHello();
            Persons.DisplayMessage("We are in the debugging mode");
            #endregion

            #region Attributes : Custom

            //reflection
            MemberInfo info       = typeof(People);
            object[]   attributes = info.GetCustomAttributes(true);
            for (int i = 0; i < attributes.Length; i++)
            {
                Console.WriteLine(attributes[i]);
            }
            #endregion

            #region GUID
            Guid guid = Guid.NewGuid();
            Console.WriteLine(guid);
            guid = Guid.NewGuid();
            Console.WriteLine(guid);
            #endregion

            #region StringBuilder
            StringBuilder builder = new StringBuilder(10, 50);
            builder.Append("John welcome back");
            Console.WriteLine(builder);
            builder.Insert(0, "Hello ");
            Console.WriteLine(builder);
            builder.Remove(18, 5);
            Console.WriteLine(builder);
            builder.Replace('o', '0');
            Console.WriteLine(builder);
            builder.Replace('e', '1', 0, 15);
            string text = builder.ToString();
            Console.WriteLine(text);
            #endregion

            #region Pattern Matching : Main
            Dog max = new Dog();
            max.DogName = "Max";
            Lion   simba      = new Lion();
            Snake  solidSnake = new Snake();
            Lizard liz        = new Lizard();
            AnimalSoundWithSwitchAndWhen(max);
            //AnimalSoundWithSwitch(simba);
            //AnimalSoundWithSwitch(solidSnake);
            //AnimalSoundWithSwitch(liz);
            #endregion

            #region Pass by ref
            //passing by value (using a copy)
            //passing by reference (using the variable itself)
            int     X1 = 3;
            ref int X2 = ref X1;//same int regardless of which one is changed (ref local)