What are generic methods in C#?

Ankith Reddy
Updated on 20-Jun-2020 16:15:03

197 Views

Generics allow you to write a class or method that can work with any data type. Declare a generic method with a type parameter −static void Swap(ref T lhs, ref T rhs) {}To call the above shown generic method, here is an example −Swap(ref a, ref b);Let us see how to create a generic method in C# −Example Live Demousing System; using System.Collections.Generic; namespace Demo {    class Program {       static void Swap(ref T lhs, ref T rhs) {          T temp;          temp = lhs;          lhs ... Read More

HashSet in C#

George John
Updated on 20-Jun-2020 16:13:36

577 Views

HashSet in C# eliminates duplicate strings or elements in an array.In C#, it is an optimized set collection.Let us see an example to remove duplicate strings using C# HashSet. Here, we have duplicate elements −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       string[] arr1 = {          "bus",          "truck",          "bus",          "car",          "truck"       };       Console.WriteLine(string.Join(", ", arr1));       // HashSet ... Read More

What are the C++ features missing in C#?

Chandu yadav
Updated on 20-Jun-2020 16:12:29

178 Views

C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg.C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.The following are some of the features of C++ missing in C# −In C#, Multiple Inheritance is not possible, whereas C++ can easily implement Multiple Inheritance.In C++, you need to manage memory manually and must allocate and deallocate memory for your objects.C++ can create standalone applications, whereas C# ... Read More

Computer Storage Definitions and Notations

Alex Onsman
Updated on 20-Jun-2020 16:11:08

664 Views

Computer storage contains many components that are used to store computer data. Some information about the various storage devices is given below.Computer Storage DefinitionsThe computer storage devices include Primary and Secondary Storage devices. These are explained in detail as follows −Primary Storage DevicesPrimary storage is also known as the main memory and is the memory directly accessible by the CPU. Some primary storage devices are −ROMROM is read only memory. This memory cannot be changed, it can only be read as required. Since ROM is unchangeable memory, it is used by data and programs that are frequently required and seldom ... Read More

Trigonometric Functions in C#

Chandu yadav
Updated on 20-Jun-2020 16:10:23

2K+ Views

Trignometric Functions in C# include, ACos, ASin, Sin, Cos, Tan, etc. It comes under the Math type of the System namespace.The following is an example showing how to implement trigonometric functions in C# −Example Live Demousing System; class Program {    static void Main() {       Console.WriteLine(Math.Acos(0));       Console.WriteLine(Math.Cos(2));       Console.WriteLine(Math.Asin(0.2));       Console.WriteLine(Math.Sin(2));       Console.WriteLine(Math.Atan(-5));       Console.WriteLine(Math.Tan(1));    } }Output1.5707963267949 -0.416146836547142 0.201357920790331 0.909297426825682 -1.37340076694502 1.5574077246549Above we saw the Inverse Sine value using Asin −Math.Asin(0.2)With that, we also saw the Inverse Cosine value using Acos −Math.Acos(0)In the ... Read More

How to capture file not found exception in C#?

Arjun Thakur
Updated on 20-Jun-2020 16:09:05

203 Views

The file not found exception is raised when you try to find a file that does not exist.Let’s say I have set a file in StreamReader, “new.txt” that does not exist. If you will try to access it using StreamReader(to read it), it will throw FileNotFoundException −using (StreamReader sReader = new StreamReader("new.txt")) { sReader.ReadToEnd(); }To handle it, you need to use try and catch −Try {    using (StreamReader sReader = new StreamReader("new.txt")) {       sReader.ReadToEnd();    }    }catch (FileNotFoundException e) {       Console.WriteLine("File Not Found!");       Console.WriteLine(e);    }

How to capture out of memory exception in C#?

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

1K+ 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

How to capture index out of range exception in C#?

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

1K+ 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

How to delete/remove an element from a C# array?

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

9K+ 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

How to control for loop using break and continue statements in C#?

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

122 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

Advertisements