Found 2628 Articles for Csharp

Complex Numbers in C#

karthikeya Boyini
Updated on 23-Jun-2020 09:17:01

2K+ Views

To work WITH and display complex numbers in C#, you need to check for real and imaginary values.A complex number like 7+5i is formed up of two parts, a real part 7, and an imaginary part 5. Here, the imaginary part is the multiple of i.To display complete numbers, use the −public struct ComplexTo add both the complex numbers, you need to add the real and imaginary part −public static Complex operator +(Complex one, Complex two) {    return new Complex(one.real + two.real, one.imaginary + two.imaginary); }You can try to run the following code to work with complex numbers in ... Read More

C# Enum GetValues Method

Arjun Thakur
Updated on 23-Jun-2020 09:18:16

1K+ Views

Get the array of the values of the constants in a specified enumeration.Here is our enum.enum Rank { Jack = 10, Tom = 19, Tim = 26 };Now, get all the values of the enum as an array and display using GetValues() method.foreach(int res in Enum.GetValues(typeof(Rank))) {    Console.WriteLine(res); }Let us see the entire example.Example Live Demousing System; public class Demo {    enum Rank { Jack = 10, Tom = 19, Tim = 26 };    public static void Main() {       Console.WriteLine("Here are the university rank of MCA Students College ABC:");       foreach(int res in ... Read More

C# Program to get the type of the specified Enumeration

Samual Sam
Updated on 23-Jun-2020 09:18:47

109 Views

Use the GetType() method to get the type of the enumeration.The enumeration.Enum[] values = { ConsoleColor.Blue, DayOfWeek.Sunday};Now to get the type, use the GetType() method.Type enumType = val.GetType();The following is an example that displays the type.Example Live Demousing System; public class Demo {    public static void Main() {       Enum[] values = { ConsoleColor.Blue, DayOfWeek.Sunday};       Console.WriteLine("{0, -5} {1, 10} {2, 10}", "Member", "Enumeration", "UnderlyingType");       foreach (var val in values)       Info(val);    }    static void Info(Enum val) {       Type enumType = val.GetType();       Type ... Read More

C# Enum GetNames Method

Chandu yadav
Updated on 23-Jun-2020 09:19:20

817 Views

The GetNames() returns the array of names of the constants in the Enumeration.The following is the enum.enum Stock { Watches, Books, Grocery };To get the array of names, use the GetNames() and loop through as shown below −foreach(string s in Enum.GetNames(typeof(Stock))) { }Let us see the complete example now.Example Live Demousing System; class Demo {    enum Stock { Watches, Books, Grocery };    static void Main() {       Console.WriteLine("The value of first stock category = {0}", Enum.GetName(typeof(Stock), 0));       Console.WriteLine("The value of second stock category = {0}", Enum.GetName(typeof(Stock), 1));       Console.WriteLine("The value of third ... Read More

C# Enum GetName Method

karthikeya Boyini
Updated on 23-Jun-2020 09:19:43

1K+ Views

The GetName() method returns the names of the constants in the Enumeration.Here is the enum.enum Stock { Appliance, Clothing, Footwear };Now, get the names using the Enum.GetName() method. Just set the constant and retrieve the individual name.Enum.GetName(typeof(Stock), 1Let us see the example now.Example Live Demousing System; class Demo {    enum Stock { Appliance, Clothing, Footwear };    static void Main() {       Console.WriteLine("The value of second stock category = {0}", Enum.GetName(typeof(Stock), 1));       Console.WriteLine("The value of third stock category = {0}", Enum.GetName(typeof(Stock), 2));    } }OutputThe value of second stock category = Clothing The value of ... Read More

Create a Quadruple Tuple in C#

George John
Updated on 23-Jun-2020 09:08:28

181 Views

Quadruple is a tuple with four items.Create a tuple first.var myTuple = Tuple.Create(100, 200, 300, 400);Above, we have created a tuple with four items i.e. Quadruple. Now to access all the four items.myTuple.Item1 myTuple.Item2 myTuple.Item3 myTuple.Item4Example Live Demousing System; public class Program {    public static void Main() {       var myTuple = Tuple.Create(100, 200, 300, 400);       Console.WriteLine("Item1 : "+ myTuple.Item1);       Console.WriteLine("Item2 : "+ myTuple.Item2);       Console.WriteLine("Item3 : "+ myTuple.Item3);       Console.WriteLine("Item4 : "+ myTuple.Item4);    } }OutputItem1 : 100 Item2 : 200 Item3 : 300 Item4 : 400

Tuple Rest Property in C#

Samual Sam
Updated on 23-Jun-2020 09:08:58

209 Views

Create tuples of eight or more elements by nesting tuple objects in the Rest property.The tuple would look like −TupleAbove, the 8th element is added using Rest property.Let us see an example.Example Live Demousing System; public class Program {    public static void Main() {       var myTuple = Tuple.Create(1, 2.5M, "Tom", "100", 5, 10.5M, "Henry", "100");       Console.WriteLine("Item1 : "+ myTuple.Item1);       Console.WriteLine("Item2 : "+ myTuple.Item2);       Console.WriteLine("Item3 : "+ myTuple.Item3);       Console.WriteLine("Item4 : "+ myTuple.Item4);       Console.WriteLine("Item5 : "+ myTuple.Item5);       Console.WriteLine("Item6 : "+ myTuple.Item6); ... Read More

C# Program to access tuple elements

Ankith Reddy
Updated on 23-Jun-2020 09:09:46

258 Views

Create a tuple.var myTuple = Tuple.Create(1, 2.5M, "Amit", "100");Now to access tuple elements, use the properties.To access first element.myTuple.Item1To access second element.myTuple.Item2In the same way, for other elements, use the properties as shown below −Example Live Demousing System; public class Program {    public static void Main() {       var myTuple = Tuple.Create(1, 2.5M, "Amit", "100");       Console.WriteLine("Item1 : "+ myTuple.Item1);       Console.WriteLine("Item2 : "+ myTuple.Item2);       Console.WriteLine("Item3 : "+ myTuple.Item3);       Console.WriteLine("Item4 : "+ myTuple.Item4);    } }OutputItem1 : 1 Item2 : 2.5 Item3 : Amit Item4 : 100

Understanding IndexOutOfRangeException Exception in C#

karthikeya Boyini
Updated on 13-Apr-2020 12:31:00

114 Views

It occurs when Index is outside the bounds of the array.Let us see an example. We have declare an array with 5 elements and set the size as 5.int[] arr = new int[5]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50;Now, we try to add the value of an element that extends the size of our array i.e.arr[5] = 60;Above, we are trying to add element at 6th position.Example Live Demousing System; using System.IO; using System.Collections.Generic; namespace Demo {    class Program {       static void Main(string[] args) {       ... Read More

C# Program to return specified number of elements from the beginning of a sequence

Arjun Thakur
Updated on 23-Jun-2020 09:10:41

110 Views

Set an array and arrange it in descending order using OrderByDescending.int[] prod = { 290, 340, 129, 540, 456, 898, 765, 789, 345};Now, use the Take() method to return specified number of elements from the beginning.Enumerable units = prod.AsQueryable().OrderByDescending(s => s).Take(2);Let us see the complete code.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] prod = { 290, 340, 129, 540, 456, 898, 765, 789, 345};       // Volume of top two products       IEnumerable units = prod.AsQueryable().OrderByDescending(s => s).Take(2);       foreach (int res in units) {          Console.WriteLine(res);       }    } }Output898 789

Advertisements