Server Side Programming Articles - Page 2517 of 2650

What are the different ways for a method to be overloaded in C#?

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

238 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

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

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

238 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

How to delete/remove an element from a C# 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

How to 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

How to 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

How to capture file not found exception in C#?

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

397 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);    }

Trigonometric Functions in C#

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

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

What is serialization in C#.NET?

Samual Sam
Updated on 20-Jun-2020 15:58:25

706 Views

Serialization converts objects into a byte stream and brings it to a form that it can be written on stream. This is done to save it to memory, file or database.Serialization can be performed as −Binary SerializationAll the members, even members that are read-only, are serialized.XML SerializationIt serializes the public fields and properties of an object into XML stream conforming to a specific XML Schema definition language document.Let us see an example. Firstly set the stream −FileStream fstream = new FileStream("d:ew.txt", FileMode.OpenOrCreate); BinaryFormatter formatter=new BinaryFormatter();Now create an object of the class and call the constructor which has three parameters −Employee ... 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

How to 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