Found 35164 Articles for Programming

Why is f required while declaring floats in C#?

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

551 Views

The f is a lowercase suffix set while declaring a float. It tells the compiler that the literal is of a specific type.Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       float val = 30.22f;       Console.WriteLine(val);    } }Output30.22Above, we have set the float using f suffix.IEnumerable res = val1.AsQueryable().Intersect(val2);

C# Queryable LongCount Method

Chandu yadav
Updated on 23-Jun-2020 07:01:10

108 Views

Use the Linq LongCount method to get the count of elements.The following is our string array −string[] emp = { "Jack", "Mark"};Now, use the LongCount() method.emp.AsQueryable().LongCount();Here is the complete code.Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       string[] emp = { "Jack", "Mark"};       long res = emp.AsQueryable().LongCount();       Console.WriteLine("{0} employees in the Department", res);    } }Output2 employees in the Department

C# Linq Distinct() Method

George John
Updated on 22-Jun-2020 15:42:04

451 Views

To get the distinct elements, use the Distinct() method.The following is our list with duplicate elements.List points = new List { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };Now to get the distinct elements −points.AsQueryable().Distinct();Let us see the entire example −Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       List points = new List { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };       // distict elements from the list       IEnumerable res = points.AsQueryable().Distinct();       foreach (int a in res) {          Console.WriteLine(a);       }    } }Output5 10 20 30 40 50 60 70

Remove all duplicates from a given string in C#

Ankith Reddy
Updated on 22-Jun-2020 15:43:10

274 Views

Here is the string.string str = "ppqqrr";Now, use Hashset to map the string to char. This will remove the duplicate characters from a string.var res = new HashSet(str);Let us see the complete example −Example Live Demousing System; using System.Linq; using System.Collections.Generic; namespace Demo {    class Program {       static void Main(string[] args) {          string str = "ppqqrr";          Console.WriteLine("Initial String: "+str);          var res = new HashSet(str);          Console.Write("New String after removing duplicates:");          foreach (char c in res){             Console.Write(c);          }         }    } }OutputInitial String: ppqqrr New String after removing duplicates:pqr

C# All method

Arjun Thakur
Updated on 22-Jun-2020 15:43:53

2K+ Views

The All Method checks for all the values in a collection and returns a Boolean. Even if one of the element do not satisfy the set condition, the All() method returns False.Let us see an example −int[] arr = {10, 15, 20};Now, using All() method, we will check whether each element in the above array is greater than 5 or not.arr.AsQueryable().All(val => val > 5);Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] arr = {10, 15, 20};       // checking if all the array elements are greater than 5       bool res = arr.AsQueryable().All(val => val > 5);       Console.WriteLine(res);    } }OutputTrue

Represent Int64 as a Binary string in C#

Chandu yadav
Updated on 22-Jun-2020 15:44:15

927 Views

To represent Int64 as a Binary string in C#, use the ToString() method and set the base as the ToString() method’s second parameter i.e. 2 for Binary.Int64 represents a 64-bit signed integer.Firstly, set an Int64 variable.long val = 753458;Now, convert it to binary string by including 2 as the second parameter.Convert.ToString(val, 2)Example Live Demousing System; class Demo {    static void Main() {       long val = 753458;       Console.WriteLine("Long: "+val);       Console.Write("Binary String: "+Convert.ToString(val, 2));    } }OutputLong: 753458 Binary String: 10110111111100110010

C# Aggregate() method

George John
Updated on 22-Jun-2020 15:44:36

429 Views

The Aggregate() method applies an accumulator function over a sequence.The following is our array −string[] arr = { "DemoOne", "DemoTwo", "DemoThree", "DemoFour"};Now use the Aggregate() method. We have set the ssed value as “DemoFive” for comparison.string res = arr.AsQueryable().Aggregate("DemoFive", (longest, next) => next.Length > longest.Length ? next : longest, str => str.ToLower());Here, the resultant string should have more number of characters than the initial seed value i.e. “DemoFive”.Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       string[] arr = { "DemoOne", "DemoTwo", "DemoThree", "DemoFour"};       string res = arr.AsQueryable().Aggregate("DemoFive", (longest, next) ... Read More

Enumerable.Repeat method in C#

Ankith Reddy
Updated on 22-Jun-2020 15:45:10

2K+ Views

Enumerable.Repeat method is part of System.Linq namespace.It returns a collection with repeated elements in C#.Firstly, set which element you want to repeat and how many times.As an example, let us see how to repeat number 10, five times −Enumerable.Repeat(10, 5);The following is the complete example −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       var val = Enumerable.Repeat(10, 5);       foreach (int res in val) {          Console.WriteLine(res);       }    } }Output10 10 10 10 10

Convert.ToUInt16 Method in C#

Arjun Thakur
Updated on 22-Jun-2020 15:45:35

166 Views

Use the Convert.ToUInt16 method to convert a specified value to a 16-bit unsigned integer.Here is our string −string str = "1129";Now let us convert it to 16-bit unsigned integer.ushort res; res = Convert.ToUInt16(str);The following is the complete example −Example Live Demousing System; public class Demo {    public static void Main() {       string str = "1307";       ushort res;       res = Convert.ToUInt16(str);       Console.WriteLine("Converted string '{0}' to {1}", str, res);    } }OutputConverted string '1307' to 1307

Convert.ToInt16 Method in C#

Chandu yadav
Updated on 22-Jun-2020 15:46:02

576 Views

Convert a specified value to a 16-bit signed integer using the Convert.ToInt16 method in C#.We have a double variable with a value initialized to it.double doubleNum = 3.456;Now, let us convert it to Int16 i.e. short.short shortNum; shortNum = Convert.ToInt16(doubleNum);Here is the complete example −Example Live Demousing System; public class Demo {    public static void Main() {       double doubleNum = 3.456;       short shortNum;       shortNum = Convert.ToInt16(doubleNum);       Console.WriteLine("Converted {0} to {1}", doubleNum, shortNum);    } }OutputConverted 3.456 to 3

Advertisements