Programming Articles - Page 3152 of 3363

How to capture file not found exception in C#?

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

412 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

728 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

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

What does the interface IStructuralEquatable do in C#?

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

453 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

What are Tuples in C#4.0?

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

859 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

What is late binding in C#?

Samual Sam
Updated on 20-Jun-2020 15:48:03

1K+ Views

In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time. Dynamic polymorphism is what we call late binding.Dynamic polymorphism is implemented by abstract classes and virtual functions. The following is an example showing an example of dynamic polymorphism −Example Live Demousing System; namespace PolymorphismApplication {    class Shape {       protected int width, height;       public Shape( int a = 0, int b = 0) {          width = a;          height = b;       ... Read More

What is a parameterized constructor in C# programs?

Chandu yadav
Updated on 20-Jun-2020 15:52:22

3K+ Views

In a constructor you can also add parameters. Such constructors are called parameterized constructors. This technique helps you to assign initial value to an object at the time of its creation.The following is an example −// class class DemoParameterized constructor with a prarameter rank −public Demo(int rank) { Console.WriteLine("RANK = {0}", rank); }Here is the complete example displaying how to work with parameterized constructor in C# −Example Live Demousing System; namespace Demo {    class Line {       private double length; // Length of a line             public Line(double len) { //Parameterized constructor ... Read More

Advertisements