DictionaryDictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.To declare a Dictionary −IDictionary d = new Dictionary();To add elements −IDictionary d = new Dictionary(); d.Add(1,97); d.Add(2,89); d.Add(3,77); d.Add(4,88);ArrayArray stores a fixed-size sequential collection of elements of the same type. It consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.To define Arrays −int[] arr = new int[5]; To initialize and set elements to Arrays.int[] arr = new int[10] {3, 5, 35, 87, 56, 99, 44, 36, 78};
For multiple-base conversions, set a variable and add the base you want to calculate.Here, for our example, I have set the variable baseNum as 2 −int baseNum = 2;In the same way, if you want base 8, then set the above as −int baseNum = 2; You can also get the above variable value as user input.After getting the value, set a stack and get the values −Stack s = new Stack(); do { s.Push(n % baseNum); n /= baseNum; } while (n != 0);After using the stack, pop out the elements. That would give you the result.Let’s say the ... Read More
Numeric promotion as the name suggests is the promotion of smaller types to larger types like short to int.In the below example, we have seen numeric promotion in Arithmetic Operator multiply.The short types are automatically promoted to larger types −Exampleusing System; class Program { static void Main() { short val1 = 99; ushort val2 = 11; int res = val1 * val2; Console.WriteLine(res); } }
Numeric promotion is the promotion of smaller types to larger types like short to int.In the below example, we have seen a numeric promotion in Conditional Expression.The short types are automatically promoted to larger type int.Exampleusing System; class Program { static void Main() { short val1 = 99; int val2; val2 = (val1 == 1) ? 100 : 30; Console.WriteLine(val2); } }OutputAbove, we have used a conditional expression that automatically promoted to int −val2 = (val1 == 1) ? 100 : 30;Here, val2 is an int and val is a short.
There are two types of collections in C#: non-generic collections and generic collections.Generics in C#Generic collections hold elements of same datatypes.For example −ListDictionaryHashsetDictionary − Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.Hashset − HashSet in C# eliminates duplicate strings or elements in an array.In C#, it is an optimized set collection.Non-Generics in C#Non-generic collections hold elements of different datatypes.The following are the non-generic collections: ArrayList, BitArray.ArrayList − It represents ordered collection of an object that can be indexed individually. ArrayList is an alternative to an array. However, unlike array you ... Read More
Internal keyword allows you to set internal access specifier.Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly.Any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.Exampleusing System; namespace RectangleApplication { class Rectangle { internal double length; internal double width; double GetArea() { return length * width; } public ... Read More
While declaring a method, if you are not sure of the number of arguments passed as a parameter, then use the C# param arrays.The following is the complete example to learn how to implement param in C# −Exampleusing System; namespace Program { class ParamArray { public int AddElements(params int[] arr) { int sum = 0; foreach (int i in arr) { sum += i; } return sum; } } class Demo { static void Main(string[] args) { ParamArray app = new ParamArray(); int sum = app.AddElements(300, 250, 350, 600, 120); Console.WriteLine("The sum is: {0}", sum); Console.ReadKey(); } } }
Arrays were a pointer to an address in memory of the index. This index was the 1st element of the array. Here, the index is like an offset and the concept even before C language originated.Let’s say your array elements begins from 0Xff000 and has 5 elements like {35, 23, 67, 88, 90}. Therefore, you array in memory would be like the following because int is stored using 4 bytes.0Xff000 has 35 0Xff004 has 23 0Xff008 has 67 0Xff012 has 88 0Xff016 has 90That would mean when the array is accessed, zero offsets would be index 0.Let us further see ... Read More
Tuple comparison came after C# 7.3.Easily compare two tuples using the equality operator in C#.Let’s say we have two tuples −var one = (x: 1, y: 2); var two = (p: 1, 2: 3, r: 3, s:4);To compare them, just use the == operator −if (one == two) Console.WriteLine("Both the tuples are same (values are same).");Let use see the code −Examplevar one = (x: 1, y: 2); var two = (p: 1, 2: 3, r: 3, s:4); if (one == two) Console.WriteLine("Both the tuples are same (values are same)."); lse Console.WriteLine("Both the tuples values are not same.");
Firstly, set the original array −int[] arr = { 1, 2,3 }; // Original Array Console.WriteLine("Original Array= "); fo reach (int i in arr) { Console.WriteLine(i); }Now, use the Array.reverse() method to reverse the array −Array.Reverse(arr);The following is the complete code to reverse an array in C# −Exampleusing System; class Demo { static void Main() { int[] arr = { 9, 32, 87, 45, 77, 56 }; // Original Array Console.WriteLine("Original Array= "); foreach (int i in arr) { Console.WriteLine(i); } // Reverse Array Array.Reverse(arr); Console.WriteLine("Reversed Array= "); foreach (int j in arr) { Console.WriteLine(j); } Console.ReadLine(); } }
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP