wingman007

C# Methods, Built-in .NET Classes, Arrays, Lists, Dictionary

Dec 5th, 2015
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Net;
  6.  
  7. class DemoLive
  8. {
  9.     static void Main()
  10.     {
  11.         /*
  12.         // int[] myArray;
  13.         // int[] myArray = new int[5];
  14.         //int[] myArray = new int[3] { 1, 2, 3 };
  15.         int[] myArray = { 1, 2, 3};
  16.  
  17.         myArray[1] = 120;
  18.  
  19.         for (var i = 0; i < myArray.Length; i++)
  20.         {
  21.             Console.WriteLine(myArray[i]);
  22.         }
  23.  
  24.         Console.WriteLine(myArray.Sum());
  25.         Console.WriteLine(myArray.Min());
  26.         Console.WriteLine(myArray.Max());
  27.         Console.WriteLine(myArray.First<int>());
  28.         Console.WriteLine(myArray.Last<int>());
  29.         */
  30.  
  31.         /*
  32.         string[] ourNames = { "Stoyan", "Ivan", "Petko" };
  33.         ourNames[1] = "Zweda";
  34.  
  35.         foreach (string name in ourNames)
  36.         {
  37.             Console.WriteLine(name);
  38.         }
  39.         */
  40.         /*
  41.         int[,] my2DimensionsArray;
  42.         // Ask the user for the size
  43.         int width = int.Parse(Console.ReadLine());
  44.         int height = int.Parse(Console.ReadLine());
  45.         my2DimensionsArray = new int[width, height];
  46.         */
  47.         /*
  48.         // int[,] my2DimensionsArray = new int[3, 3];
  49.         int[,] my2DimensionsArray = new int[3, 3] {
  50.             {1,2,3 },
  51.             {1,2,3 },
  52.             {1,2,3 }
  53.         };
  54.  
  55.         my2DimensionsArray[0, 1] = 120;
  56.  
  57.         for(var i = 0; i < my2DimensionsArray.GetLength(0); i++)
  58.         {
  59.             for (var j=0; j< my2DimensionsArray.GetLength(1); j++)
  60.             {
  61.                 Console.Write("{0,10}",my2DimensionsArray[i,j]);
  62.             }
  63.             Console.WriteLine();
  64.         }
  65.         */
  66.         /*
  67.         int[,,] my3DimensionsArray = new int[3,3,3]
  68.         {
  69.             { {1,2,3 }, { 1,2,3 }, {1,2,3 } },
  70.             { {1,2,3 }, { 1,2,3 }, {1,2,3 } },
  71.             { {1,2,3 }, { 1,2,3 }, {1,2,3 } }
  72.         };
  73.         */
  74.         /*
  75.         int[][] myArrayOfArrays = {
  76.             new int[2] {1,2},
  77.             new int[3] {1,2,3},
  78.             new int[4] {1,2,3,4},
  79.             new int[2] {1,2}
  80.         };
  81.         */
  82.         /*
  83.         int width = 4, height = 6;
  84.         string[,] matrix =
  85.           new string[height, width];
  86.         for (int row = 0; row < height; row++)
  87.         {
  88.             for (int col = 0; col < width; col++)
  89.             {
  90.                 matrix[row, col] = "" +
  91.                   (char)('a' + row) +
  92.                   (char)('a' + col);
  93.             }
  94.         }
  95.  
  96.         for (int row = 0; row < height; row++)
  97.         {
  98.             for (int col = 0; col < width; col++)
  99.             {
  100.                 Console.Write("{0,10}",matrix[row, col]);
  101.             }
  102.             Console.WriteLine();
  103.         }
  104.         */
  105.         /*
  106.         for (int i = 0; i < 10; i++)
  107.         {
  108.             //for (var j = 0; j < i; j++)
  109.             //{
  110.             //    Console.Write("-");
  111.             //}
  112.             // PrintHyphens(i, 2,3,4,4);
  113.             PrintHyphens(3, j : i);
  114.             Console.WriteLine();
  115.         }
  116.     }
  117.  
  118.     public static void PrintHyphens(int t, int n =10, int j = 4) //  params int[] myParams)
  119.     {
  120.         for (var j = 0; j < n; j++)
  121.         {
  122.             // Console.Write("{0}, {1}", 2,3);
  123.             Console.Write("-");
  124.         }
  125.         */
  126.         /*
  127.         Console.WriteLine(CalcTriangleArea(2.3, 4.5));
  128.         Console.WriteLine(CalcTriangleArea(10, 4.5));
  129.         Console.WriteLine(CalcTriangleArea(23, 4.5));
  130.         */
  131.         /*
  132.         DateTime today = DateTime.Now;
  133.         today = today.AddDays(2);
  134.         Console.WriteLine(today);
  135.  
  136.         double cosine =Math.Cos(Math.PI* 60 /180 );
  137.         Console.WriteLine(cosine);
  138.         */
  139.         /*
  140.         Random rnd = new Random();
  141.         for (int i = 0; i < 10; i++)
  142.         {
  143.             Console.WriteLine(rnd.Next(1, 10));
  144.         }
  145.         */
  146.         /*
  147.         DateTime today = DateTime.Now;
  148.         Console.WriteLine("Today is: " + today);
  149.         DateTime tomorrow = today.AddDays(1);
  150.         Console.WriteLine("Tomorrow is: " + tomorrow);
  151.         double angleDegrees = 60;
  152.         double angleRadians = angleDegrees * Math.PI / 180;
  153.         Console.WriteLine(Math.Cos(angleRadians));
  154.         Random rnd = new Random();
  155.         Console.WriteLine(rnd.Next(1, 100));
  156.         WebClient webClient = new WebClient();
  157.         webClient.DownloadFile("http://example.com", "file.txt");
  158.         Process.Start("file.txt");
  159.         */
  160.  
  161.         /*
  162.         List<int> numbers = new List<int>();
  163.         numbers.Add(1);
  164.         numbers.Add(2);
  165.         numbers.Add(3);
  166.  
  167.         for (int i = 0; i < numbers.Count; i++)
  168.         {
  169.             Console.WriteLine(numbers[i]);
  170.         }
  171.         */
  172.         /*
  173.         List<string> names =
  174.     new List<string>() { "Peter", "Maria", "Katya", "Todor" };
  175.         names.Add("Nakov"); // Peter, Maria, Katya, Todor, Nakov
  176.         names.RemoveAt(0); // Maria, Katya, Todor, Nakov
  177.         names.Insert(3, "Sylvia"); // Maria, Katya, Todor, Sylvia, Nakov
  178.         names[1] = "Michael"; // Maria, Michael, Todor, Sylvia, Nakov
  179.         foreach (var name in names)
  180.         {
  181.             Console.WriteLine(name);
  182.         }
  183.         */
  184.         /*
  185.         Dictionary<string, string> phonebook = new Dictionary<string, string>();
  186.         phonebook["John Smith"] = "+1-555-8976";
  187.         phonebook["Lisa Smith"] = "+1-555-1234";
  188.         phonebook["Sam Doe"] = "+1-555-5030";
  189.         phonebook["Nakov"] = "+359-899-555-592";
  190.         phonebook["Nakov"] = "+359-2-981-9819";
  191.         phonebook.Remove("John Smith");
  192.         foreach (var pair in phonebook)
  193.         {
  194.             Console.WriteLine("{0} --> {1}", pair.Key, pair.Value);
  195.         }
  196.         */
  197.         /*
  198.         Dictionary<DateTime, string> events =
  199.     new Dictionary<DateTime, string>();
  200.         events[new DateTime(1998, 9, 4)] = "Google's birth date";
  201.         events[new DateTime(2013, 11, 5)] = "SoftUni's birth date";
  202.         events[new DateTime(1975, 4, 4)] = "Microsoft's birth date";
  203.         events[new DateTime(2004, 2, 4)] = "Facebook's birth date";
  204.         events[new DateTime(2013, 11, 5)] =
  205.             "Nakov left Telerik Academy to establish SoftUni";
  206.         foreach (var entry in events)
  207.         {
  208.             Console.WriteLine("{0:dd-MMM-yyyy}: {1}",
  209.                 entry.Key, entry.Value);
  210.         }
  211.         */
  212.         /*
  213.         string str = "SoftUni";
  214.         Console.WriteLine(str);
  215.         for (int i = 0; i < str.Length; i++)
  216.         {
  217.             Console.WriteLine("str[{0}] = {1}", i, str[i]);
  218.         }
  219.         Console.WriteLine(str.IndexOf("Uni")); // 4
  220.         Console.WriteLine(str.IndexOf("uni")); // -1 (not found)
  221.         Console.WriteLine(str.Substring(4, 3)); // Uni
  222.         Console.WriteLine(str.Replace("Soft", "Hard")); // HardUni
  223.         Console.WriteLine(str.ToLower()); // softuni
  224.         Console.WriteLine(str.ToUpper()); // SOFTUNI
  225.         */
  226.         /*
  227.         string firstName = "Steve";
  228.         string lastName = "Jobs";
  229.         int age = 56;
  230.         Console.WriteLine(firstName + " " + lastName +
  231.           " (age: " + age + ")"); // Steve Jobs (age: 56)
  232.         string allLangs = "C#, Java; HTML, CSS; PHP, SQL";
  233.         string[] langs = allLangs.Split(new char[] { ',', ';', ' ' },
  234.           StringSplitOptions.RemoveEmptyEntries);
  235.         foreach (var lang in langs)
  236.             Console.WriteLine(lang);
  237.         Console.WriteLine("Langs = " + string.Join(", ", langs));
  238.         Console.WriteLine("  \n\n Software  University  ".Trim());
  239.         */
  240.  
  241.         Person[] persons = new Person[2];
  242.  
  243.         Person stoyan = new Person();
  244.         stoyan.Name = "Stoyan";
  245.         stoyan.Age = 52;
  246.  
  247.         Person mariya = new Person();
  248.         mariya.Name = "Mariya";
  249.         mariya.Age = 20;
  250.  
  251.         // Console.WriteLine("is am a human. My name is {0}. I am {1} years old!", stoyan.Name, stoyan.Age);
  252.         // Console.WriteLine("is am a human. My name is {0}. I am {1} years old!", mariya.Name, mariya.Age);
  253.  
  254.         stoyan.Introduce();
  255.         mariya.Introduce();
  256.  
  257.         persons[0] = stoyan;
  258.  
  259.     }
  260.  
  261.     public static double CalcTriangleArea(double width, double height)
  262.     {
  263.         return width * height / 2;
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment