Found 35164 Articles for Programming

Return the total elements in a sequence as a 64-bit signed integer in C#

karthikeya Boyini
Updated on 23-Jun-2020 07:08:30

49 Views

Firstly, set a string array.string[] num = { "One", "Two", "Three", "Four", "Five"};Use the Linq LongCount method to get the count of elements.num.AsQueryable().LongCount();Here is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       string[] num = { "One", "Two", "Three", "Four", "Five"};       long res = num.AsQueryable().LongCount();       Console.WriteLine("{0} elements", res);    } }Output5 elements

C# Program to get the smallest and largest element from a list

George John
Updated on 23-Jun-2020 07:09:34

7K+ Views

Set a list.List list = new List { 150, 300, 400, 350, 450, 550, 600 };To get the smallest element, use the Min() method.list.AsQueryable().Min();To get the largest element, use the Max() method.list.AsQueryable().Max();Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List list = new List { 150, 300, 400, 350, 450, 550, 600 };       foreach(long ele in list){          Console.WriteLine(ele);       }       // getting largest element       long max_num = list.AsQueryable().Max(); ... Read More

C# Program to display the first element from an array

Ankith Reddy
Updated on 23-Jun-2020 06:51:22

1K+ Views

The following is our array −double[] myArr = {20.5, 35.6, 45.7, 55.6, 79.7};To get the first element, use the First() method.myArr.AsQueryable().First();Let us see the complete code −Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       double[] myArr = {20.5, 35.6, 45.7, 55.6, 79.7};       double res = myArr.AsQueryable().First();       Console.WriteLine(res);    } }Output20.5

Convert Decimal to Int64 (long) in C#

Samual Sam
Updated on 23-Jun-2020 06:50:42

5K+ Views

Use the Convert.ToInt64() method to convert Decimal to Int64 (long) in C#.Let’s say we have a decimal variable.decimal d = 310.23m;Now to convert it to Int64, use the Convert.ToInt64() method.long res; res = Convert.ToInt64(d);Let us see another example −Example Live Demousing System; class Demo {    static void Main() {       decimal d = 190.66m;       long res;       res = Convert.ToInt64(d);       Console.WriteLine("Converted Decimal '{0}' to Int64 value {1}", d, res);    } }OutputConverted Decimal '190.66' to Int64 value 191

Return a C# tuple from a method

karthikeya Boyini
Updated on 23-Jun-2020 06:52:32

316 Views

Firstly, create a tuple as shown below that calls a method.var tuple = Show();The above statement calls the following method −static Tuple Show()Under the method, return the tuple as shown below −Example Live Demousing System; public class Demo {    public static void Main() {       var tuple = Show();       Console.WriteLine(tuple.Item1);       Console.WriteLine(tuple.Item2);       Console.WriteLine(tuple.Item3);       Console.WriteLine(tuple.Item4);       Console.WriteLine(tuple.Item5);    }    static Tuple Show() {       return Tuple.Create(3, 5, 7, 9, 11);    } }Output3 5 7 9 11

Set tuple as a method parameter in C#

Arjun Thakur
Updated on 23-Jun-2020 06:51:59

3K+ Views

Firstly, set a tuplevar tuple = Tuple.Create(100, 200, 300);Now, pass the tuple as a method parameter −Show(tuple);Here’s our method.static void Show(Tuple tuple)Now call the tuple values one by one as shown below −Example Live Demousing System; public class Program {    public static void Main() {       var tuple = Tuple.Create(100, 200, 300);       Show(tuple);    }    static void Show(Tuple tuple) {       Console.WriteLine(tuple.Item1);       Console.WriteLine(tuple.Item2);       Console.WriteLine(tuple.Item3);    } }Output100 200 300

Convert.ToDecimal Method in C#

Samual Sam
Updated on 23-Jun-2020 06:53:07

3K+ Views

Convert a specified value to a decimal number using the Convert.ToDecimal() method.We have a string here.string stringVal = "2,345.26";Now, let us use the Convert.ToDecimal() method to convert it to a decimal number.decimal decimalVal; decimalVal = System.Convert.ToDecimal(stringVal);Let us now see the complete example −Example Live Demousing System; public class Demo {    public static void Main() {       decimal decimalVal;       string stringVal = "2,345.26";       decimalVal = System.Convert.ToDecimal(stringVal);       System.Console.WriteLine("String converted to decimal = {0} ", decimalVal);    } }OutputString converted to decimal = 2345.26

C# Program to filter array elements based on a predicate

Chandu yadav
Updated on 23-Jun-2020 06:55:01

2K+ Views

Set an array.int[] arr = { 40, 42, 12, 83, 75, 40, 95 };Use the Where clause and predicate to get elements above 50.IEnumerable myQuery = arr.AsQueryable() .Where((a, index) => a >= 50);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[] arr = { 40, 42, 12, 83, 75, 40, 95 };       Console.WriteLine("Array:");       foreach (int a in arr) {          Console.WriteLine(a);       }       // getting elements above 70       IEnumerable myQuery = arr.AsQueryable() .Where((a, index) => a >= 50);       Console.WriteLine("Elements above 50...:");       foreach (int res in myQuery) {          Console.WriteLine(res);       }    } }OutputArray: 40 42 12 83 75 40 95 Elements above 50...: 83 75 95

Nested Tuples in C#

George John
Updated on 23-Jun-2020 06:55:41

441 Views

Let us first declare a nested tuple.var tuple = Tuple.Create(100, 200, 300, 400, 500, 600, Tuple.Create(720, 750, 780), 800 );Above, we added a nested tuple using Tuple.Create.Now to display elements in a nested tuple, nest the Item properties. Sine 7th item in the tuple is nested, we will be using the following to get the nested items −tuple.Item7.Item1; tuple.Item7.Item2; tuple.Item7.Item3;Let us see the complete code.Example Live Demousing System; public class Program {    public static void Main() {       var tuple = Tuple.Create(100, 200, 300, 400, 500, 600, Tuple.Create(720, 750, 780), 800 );       Console.WriteLine(tuple.Item1);     ... Read More

Join, Sleep and Abort methods in C# Threading

Ankith Reddy
Updated on 23-Jun-2020 06:58:34

334 Views

JoinBlocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping. This method has different overloaded forms.SleepMakes the thread pause for a period of time.AbortThe Abort method is used to destroy threads.Let us see an example of Join() in threading −Exampleusing System; using System.Diagnostics; using System.Threading; namespace Sample {    class Demo {       static void Run() {          for (int i = 0; i < 2; i++)          Console.Write("Sample text!");       }       static void Main(string[] args) {       ... Read More

Advertisements