The System.OutOfMemoryException occurs when the CLR fail in allocating enough memory that is needed.System.OutOfMemoryException is inherited from the System.SystemException class.Set the strings −string StudentName = "Tom"; string StudentSubject = "Maths";Now you need to initialize with allocated Capacity that is the length of initial value −StringBuilder sBuilder = new StringBuilder(StudentName.Length, StudentName.Length);Now, if you will try to insert additional value, the exception occurs.sBuilder.Insert(value: StudentSubject, index: StudentName.Length - 1, count: 1);The following exception occurs −System.OutOfMemoryException: Out of memoryTo capture the error, try the following code −Example Live Demousing System; using System.Text; namespace Demo { class Program { static void ... Read More
IndexOutOfRangeException occurs when you try to access an element with an index that is outsise the bounds of the array.Let’s say the following is our array. It has 5 elements −int [] n = new int[5] {66, 33, 56, 23, 81};Now if you will try to access elements with index more than 5, then the IndexOutOfRange Exception is thrown −for (j = 0; j < 10; j++ ) { Console.WriteLine("Element[{0}] = {1}", j, n[j]); }In the above example, we are trying to access above index 5, therefore the following error occurs −System.IndexOutOfRangeException: Index was outside the bounds of the array.Here ... Read More
To delete an elements from a C# array, we will shift the elements from the position the user want the element to delete.Here, first we have 5 elements −int[] arr = new int[5] {35, 50, 55, 77, 98};Now let’s say we need to delete the element at 2nd position i.e. variable “pos = 2” is set, for that shift the elements after the specified position −// Shifting elements for (i = pos-1; i < 4; i++) { arr[i] = arr[i + 1]; }Now display the result as shown in the complete code below.Example Live Demousing System; using System.Collections.Generic; using System.Linq; ... Read More
Break statement terminates the loop. To use it in a for loop, you can get input from user everytime and display the output when user enters a negative number. The output gets displayed then and exited using the break statement −for(i=1; i
The different ways with which a method can be overloaded is −The datatypes of parameters are different The number of parameters are differentThe following gives an example stating different datatypes of parameters −void print(int i) { Console.WriteLine("Printing int: {0}", i ); } void print(double f) { Console.WriteLine("Printing float: {0}" , f); } void print(string s) { Console.WriteLine("Printing string: {0}", s); }The following states the different number of parameters −// two parameters public static int mulDisplay(int one, int two) { return one * two; } // three parameters public static int mulDisplay(int one, int ... Read More
Tuples has a sequence of elements of different data types. It was introduced to return an instance of the Tuple with no need to specify the type of each element separately.Let us create a tuple with two elements. The following is how you declare a tuple. −Tupleperson = new Tuple (32, "Steve");Now, for the example check for the first item in the tuple, which is an integer −if (tuple.Item1 == 99) { Console.WriteLine(tuple.Item1); }Now check for second item in the tuple, which is a string −if (tuple.Item2 == "Steve") { Console.WriteLine(tuple.Item2); }The following is an example to create ... Read More
The IStructuralEquatable interface defines methods to support the comparison of objects for structural equality, which means that two objects are equal because they have equal values.It includes the following two methods −Sr.NoMethod & Description1Equals(Object, IEqualityComparer)The method determined whether an object is structurally equal to the current instance.2GetHashCode(IEqualityComparer)The methods a hash code for the current instance.Let us see an example in which I have created Tuple objects and worked with IstructuralEquatable interface:Create Tuples −var tupleOne = Tuple.Create(26.3, Double.NaN, 35.6); var tupleOne = Tuple.Create(26.3, Double.NaN, 35.6);Now check the equality by calling IStructuralEquatable.Equals using default comparer.IStructuralEquatable chk = tupleOne; Console.WriteLine(chk.Equals(tupleTwo, EqualityComparer.Default));Read More
The DateTime has methods and properties for Date and Time as well like how to get the number of hours or minutes of a day, etc.Let us only focus on the time functions −Refer MSDN (Microsoft Developer Network) for all the functions −Sr.No.Method & Properties1AddDays(Double)Returns a new DateTime that adds the specified number of days to the value of this instance.2AddHours(Double)Returns a new DateTime that adds the specified number of hours to the value of this instance.3AddMilliseconds(Double)Returns a new DateTime that adds the specified number of milliseconds to the value of this instance.4AddMinutes(Double)Returns a new DateTime that adds the specified ... Read More
To get intersection of two arrays, use the Intersect method. It is an extension method from the System.Linq namespace.The method returns the common elements between the two arrays.Set the two arrays first −int[] arr1 = { 44, 76, 98, 34 }; int[] arr2 = { 24, 98, 44, 55, 47, 86 };Now use the Intersect on both the arrays −Arr1.Intersect(arr2);The following is the complete code −Example Live Demousing System; using System.Linq; class Program { static void Main() { int[] arr1 = { 44, 76, 98, 34 }; int[] arr2 = { 24, 98, 44, 55, 47, 86 }; var intersect = arr1.Intersect(arr2); foreach (int res in intersect) { Console.WriteLine(res); } } }Output44 98
To find the absolute value of a number in C#, use the Math.Abs method.Set numbers first −int val1 = 77; int val2 = -88;Now take two new variables and get the Absolute value of the above two numbers −int abs1 = Math.Abs(val1); int abs2 = Math.Abs(val2);Let us see the complete code to display Absolute value of a number −Example Live Demousing System; class Program { static void Main() { int val1 = 77; int val2 = -88; Console.WriteLine("Before..."); Console.WriteLine(val1); Console.WriteLine(val2); ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP