Introduction to C#
Basic Introduction to the C# Programming Language
Updated: 03 September 2023
From the Microsoft Virtual Academy
Hello World
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6
7namespace HelloWorld8{9 class Program10 {11 static void Main(string[] args)12 {13 Console.WriteLine("Hello, World!");14 Console.ReadLine();15 }16 }17}
Variables
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6
7namespace Variables8{9 class Program10 {11 static void Main(string[] args)12 {13 Console.WriteLine("Some Math here : ");14 int x;15 int y;16
17 x = 7;18 y = x + 3;19
20 Console.WriteLine("x = 7");21 Console.WriteLine("y = x + 3");22 Console.WriteLine("y = " + y);23
24 Console.ReadLine();25
26 //-------------------------------------------27
28 Console.WriteLine("What is your name?");29 Console.Write("Type your first name : ");30 string MyFirstName = Console.ReadLine();31
32 Console.Write("Type your last name : ");33 string MyLastName = Console.ReadLine();34
35 Console.WriteLine("Hello, " + MyFirstName + " " + MyLastName + " !");36 Console.ReadLine();37
38 }39 }40}
Decisions
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6
7namespace Decisions8{9 class Program10 {11 static void Main(string[] args)12 {13 Console.WriteLine("Bob's Big Giveaway");14 Console.Write("Choose a Door : 1,2 or 3 : ");15 string Selection = Console.ReadLine();16
17 string Prize;18
19 if (Selection == "1")20 Prize = "You Win a Car";21
22 else if (Selection == "2")23 Prize = "You Win a Goat";24
25 else if (Selection == "3")26 Prize = "You Win a bag of weed";27
28 else29 {30 Prize = "nada Bruv";31 Prize += ", You Lose";32 }33
34 //----------------------------------------------35
36 Console.WriteLine(Prize);37 Console.ReadLine();38
39 Console.WriteLine("Bob's Big Giveaway Part 2");40 Console.Write("Choose a Door : 1,2 or 3 : ");41 string Selection2 = Console.ReadLine();42
43 string Prize2 = (Selection2 == "1") ? "Boat" : "Bale of Hay";44
45 Console.WriteLine("You chose {0}, you Win a {1}", Selection2, Prize2);46 Console.ReadLine();47
48
49 }50 }51}
For Loops
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6
7namespace ForIteration8{9 class Program10 {11 static void Main(string[] args)12 {13 for (int i = 0; i < 10; i++) // For loop, create variable; stop condition; increase value by one14 {15 Console.WriteLine(i);16 if (i == 7)17 {18 Console.WriteLine("Found Seven");19 break; // Breaks out of for loop20 }21 }22 Console.ReadLine();23 }24 }25}
While Loop
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6
7namespace WhileIteration8{9 class Program10 {11 static void Main(string[] args)12 {13 do14 {15 Console.Clear();16 } while (MenuOptions());17
18 }19
20 private static bool MenuOptions()21 {22 Console.WriteLine("Choose an Option:");23 Console.WriteLine("1. Write stuff");24 Console.WriteLine("2. Random Int");25 Console.WriteLine("3. Exit");26
27 string result = Console.ReadLine();28
29 if (result == "1")30 {31 Console.WriteLine("Write stuff for me to write");32 string input = Console.ReadLine();33 while (input != "")34 {35 Console.WriteLine("You Said {0}", input);36 input = Console.ReadLine();37 }38 return true;39 }40 else if (result == "2")41 {42 Random MyRandom = new Random();43 int RandomNum = MyRandom.Next(0, 10);44 Console.WriteLine("Guess the Int");45
46 bool correct;47 do48 {49 string guess = Console.ReadLine();50 if (guess == RandomNum.ToString())51 {52 correct = true;53 Console.WriteLine("Correct!");54 Console.WriteLine("Click Enter to end");55 }56 else57 {58 correct = false;59 Console.WriteLine("Incorrect");60 }61 } while (correct == false);62
63 Console.ReadLine();64 return true;65 }66 else if (result == "3")67 {68 return false;69 }70 else71 {72 return true;73 }74 }75 }76}
Arrays
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6
7namespace Arrays8{9 class Program10 {11 static void Main(string[] args)12 {13 int[] nums = new int[5];14
15 nums[0] = 4;16 nums[1] = 5;17 nums[2] = 8;18 nums[3] = 43;19 nums[4] = 91;20
21 Console.WriteLine(nums[3]);22
23 int len = nums.Length;24 Console.WriteLine(len);25
26 int[] vals = new int[] { 1, 2, 2, 8, 3, 12, 54 };27 for (int i = 0; i < vals.Length; i++) //manually iterate through values28 {29 Console.WriteLine(vals[i]);30 }31
32 string[] words = new string[] { "hey", "man", "wassup" };33 foreach (string word in words) //automatically iterate through items34 {35 Console.WriteLine(word);36 }37
38 // we can create a string and make it an array of characters39
40 string me = "Nabeel Valley"; //make string41 char[] mechars = me.ToCharArray(); //convert to CharArray42 Array.Reverse(mechars); //reverse array values43
44 foreach (char mechar in mechars)45 {46 Console.Write(mechar);47 }48
49 Console.ReadLine();50 }51 }52}
Functions
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6
7namespace SimpleMethod8{9 class Program10 {11 static void Main(string[] args)12 {13 string message = "Hello World";14 PrintStuff(message);15
16 PrintStuff("Hey who are you?");17 string name = Console.ReadLine();18 string[] PrintWords = new string[] { "Hello", name };19
20 PrintStuff(JoinWords("Hello", name)); //uses method singnature for two inputs21 PrintStuff(JoinWords(PrintWords)); //uses method signature for array input22
23 Console.ReadLine();24 }25
26 private static void PrintStuff(string text)27 {28 Console.WriteLine(text);29 }30
31 private static string JoinWords(string word1, string word2)//two string joiner32 {33 return (word1 + " " + word2);34 }35
36 private static string JoinWords(string[] words)//array string joiner37 {38 string newstring = "";39 foreach (string word in words)40 {41 newstring += word + " ";42 }43 return newstring;44 }45 }46}