Capture Out of Memory Exception in C#

Ankith Reddy
Updated on 20-Jun-2020 16:08:22

2K+ Views

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

Capture Index Out of Range Exception in C#

George John
Updated on 20-Jun-2020 16:07:25

2K+ Views

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

Delete Element from a Chash Array

Chandu yadav
Updated on 20-Jun-2020 16:06:09

11K+ Views

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

Control For Loop Using Break and Continue Statements in C#

George John
Updated on 20-Jun-2020 16:05:10

258 Views

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

Different Ways for a Method to be Overloaded in C#

Ankith Reddy
Updated on 20-Jun-2020 16:04:34

270 Views

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

What are Tuples in C#

karthikeya Boyini
Updated on 20-Jun-2020 16:04:01

867 Views

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

IStructuralEquatable Interface in C#

Samual Sam
Updated on 20-Jun-2020 16:03:02

458 Views

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

Time Functions in C#

Chandu yadav
Updated on 20-Jun-2020 16:02:46

1K+ Views

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

Intersection of Two Arrays in C#

karthikeya Boyini
Updated on 20-Jun-2020 16:01:36

5K+ Views

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

Display Absolute Value of a Number in C#

Arjun Thakur
Updated on 20-Jun-2020 16:01:08

3K+ Views

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

Advertisements