Found 2628 Articles for Csharp

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 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 file not found exception in C#?

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

243 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

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

What is serialization in C#.NET?

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

530 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

What is the difference between type conversion and type casting in C#?

karthikeya Boyini
Updated on 20-Jun-2020 15:59:33

329 Views

Type conversion and type casting are the same in C#. It is converting one type of data to another type. In C#, type casting has two forms −Implicit type conversion − These conversions are performed by C# in a type-safe manner. For example, are conversions from smaller to larger integral types and conversions from derived classes to base classes.Explicit type conversion − These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.The following is an example showing how to cast double to int −Example Live Demousing System; namespace Demo {    class Program { ... Read More

What is Regex class and its class methods in C#?

Samual Sam
Updated on 20-Jun-2020 16:00:18

151 Views

The Regex class is used for representing a regular expression. A regular expression is a pattern that could be matched against an input text.The following are the methods of Regex class −Sr.NoMethod & Description1public bool IsMatch(string input)Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input string.2public bool IsMatch(string input, int startat)Indicates whether the regular expression specified in the Regex constructor finds a match in the specified input string, beginning at the specified starting position in the string.3public static bool IsMatch(string input, string pattern)Indicates whether the specified regular expression finds a match in ... Read More

Intersection of two arrays in C#

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

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

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

Time Functions in C#

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

623 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

Advertisements