| Topic | Code |
|---|---|
| Escape character in C# | Console.WriteLine(“Hello ” World”); Console.WriteLine(“Hello \t World”); |
| String formatting | Console.WriteLine({0} , Hello); |
| Convert full string to uppercase | s.ToUpper() |
| Convert only one char to uppercase | char.ToUpper(s[0]) |
| Substring printing | Console.WriteLine(s[2..]); |
| Array initialization | int[] arr = new int[5]; or, Â int[] arr = new int[] { 1, 3, 5, 7, 9 }; or, int[] arr = { 1, 3, 5, 7, 9 }; |
| Multidimensional array | int[,] array = new int[4, 2]; |
| Jagged array | int[][] jaggedArray = new int[3][]; |
| Decimal to binary | Console.WriteLine(Convert.ToString(m, toBase: 2)); |
| Decimal to octal | Console.WriteLine(Convert.ToString(x, toBase: 8)); |
| Bitwise NOT for toggling binary bit | int x = ~x; |
| Maximum of two values | Math.Max(sum, h) |
| Ceiling division | Math.Ceiling((double)n / 2); |
| Swap values | (a, b) = (b, a); |
| HashSet | HashSet myhash.Add(“C”); myhash.Clear(); |
| List (like vector) | List list.Add(12); Console.Write(“{0} ”, list[i]); |
| Dictionary (like pair in C++) | Dictionary<string, int> figures = new Dictionary<string, int>() { “Tetrahedron”, 4}, {“Cube”, 6}, {“Octahedron”, 8}, {“Dodecahedron”, 12}, {“Icosahedron”, 20} |
| Count occurrences using Dictionary | [See the provided code] |
| String sorting in C# | String.Concat(str.OrderBy(c ⇒ c)); string SortString(string input){char[] characters = input.ToArray();Array.Sort(characters);return new string(characters);} s = SortString(s); |
| Array sorting and reversing | Array.Sort(myarr); Array.Reverse(myarr); |
| Merge two arrays | int[] arr = arr1.Concat(arr2).ToArray(); |
| Remove duplicates from an array | arr = arr.Distinct().ToArray(); |
| Maximum value in an array | int maxi = arr.Max(); |
| Length of an array | int len = arr.Length; |
| Sum of elements in an array | int total = arr.Sum(); |
| Count occurrences of a specific value in an array | int one = arr.Where(i ⇒ i == 1).Count(); |
| Lambda expressions | [See the provided code] |
| Input for 1D array | arr = Array.ConvertAll(Console.ReadLine().Split(’ ’), int.Parse); |
| Input for 2D array | [See the provided code] |
| Accessibility levels | public < internal < protected < private |
| int.Parse vs Convert.ToInt32 | [Provided explanation] |
| Check object type | if (obj is string) if (!(obj is int)) |
| Primitive types in C# | Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, Single |
| Object class methods | [Provided explanation and code] |