Found 34494 Articles for Programming

SequenceEqual method in C#

Ankith Reddy
Updated on 22-Jun-2020 14:47:37

256 Views

The SequenceEqual method is used to test collections for equality.Let us set three string arrays −string[] arr1 = { "This", "is", "it" }; string[] arr2 = { "My", "work", "report" }; string[] arr3 = { "This", "is", "it" };Now, compare the first array with the second using the SequenceEqual() method −arr1.SequenceEqual(arr2);The following is an example −Example Live Demousing System; using System.Linq; class Program {    static void Main() {       string[] arr1 = { "This", "is", "it" };       string[] arr2 = { "My", "work", "report" };       string[] arr3 = { "This", "is", "it" };       bool res1 = arr1.SequenceEqual(arr2);       Console.WriteLine(res1);       bool res2 = arr1.SequenceEqual(arr3);       Console.WriteLine(res2);    } }OutputFalse True

Any Extension method in C#

George John
Updated on 22-Jun-2020 14:47:54

358 Views

The Any() extension method is part of the System.Linq namepspace. Using this method, you can check whether any of the elements matches a certain condition or not.Firstly, set an array with elements −int[] arr = { 6, 7, 15, 40, 55 };The following is an example. It checks whether any of the element in the array is greater than and equal to 20 or not −arr.Any(element => element >= 20);Here is the complete code −Example Live Demousing System; using System.Linq; class Program {    static void Main() {       int[] arr = { 6, 7, 15, 40, 55 };       bool res = arr.Any(element => element >= 20);       Console.WriteLine(res);    } }OutputTrue

C# Program to get the first three elements from a list

Samual Sam
Updated on 22-Jun-2020 14:39:30

8K+ Views

Use the Take() method to get the first individual number of elements in C#.Firstly, set a list and add elements −List myList = new List(); myList.Add("One"); myList.Add("Two"); myList.Add("Three"); myList.Add("Four"); myList.Add("Five"); myList.Add("Six");Now, use the Take() method to get the elements from the list. Add the number of the elements you want as an argument −myList.Take(3)Here is the code −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       List myList = new List();       myList.Add("One");       myList.Add("Two");       myList.Add("Three");       myList.Add("Four");     ... Read More

C# Program to find the largest element from an array using Lambda Expressions

karthikeya Boyini
Updated on 22-Jun-2020 14:39:54

467 Views

Declare an array −int[] arr = { 10, 90, 20, 19, 99, 57 };Now to get the largest element from an array, use Max() method with lambda expressions −arr.Max());Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] arr = { 10, 90, 20, 19, 99, 57 };       Console.WriteLine(arr.Max(element => Math.Abs(element)));    } }Output99

SkipWhile method in C#

Samual Sam
Updated on 22-Jun-2020 14:40:31

284 Views

SkipWhile skips an element when a condition is matched.For example, use the following if you want to skip all even elements −ele => ele %2 == 0The following is an example wherein all the even elements are skipped and only the odd elements are displayed −Example Live Demousing System.IO; using System; using System.Linq; public class Demo {    public static void Main() {       int[] arr = { 20, 35, 55 };       Console.WriteLine("Initial array...");       foreach (int value in arr) {          Console.WriteLine(value);       }       // ... Read More

C# Program to skip initial elements in an array

karthikeya Boyini
Updated on 22-Jun-2020 14:41:15

2K+ Views

If you want to skip a number of elements in an array, then use the Skip() method in C#.Let’s say the following is our array −int[] arr = { 24, 40, 55, 62, 70, 82, 89, 93, 98 };Now to skip first four elements −var ele = arr.Skip(4);Let us see the complete example −Example Live Demousing System.IO; using System; using System.Linq; public class Demo {    public static void Main() {       int[] arr = { 24, 40, 55, 62, 70, 82, 89, 93, 98 };       Console.WriteLine("Initial Array...");       foreach (var res in arr) ... Read More

ElementAt() method in C#

Samual Sam
Updated on 22-Jun-2020 14:41:37

3K+ Views

ElementAt() is a System.Linq method in C# that is used to get and display element at a particular index.The following is our string array −string[] arr = { "One", "Two", "Three", "Four", "Five" };Now to get an element at index 0, use the ElementAt() method −arr.ElementAt(0);The following is the complete code −Example Live Demousing System.IO; using System; using System.Linq; public class Demo {    public static void Main() {       string[] arr = { "One", "Two", "Three", "Four", "Five" };       // displaying element at index 0       string res = arr.ElementAt(0);       Console.WriteLine(res);    } }OutputOne

Literal number suffixes in C#

karthikeya Boyini
Updated on 22-Jun-2020 14:42:15

268 Views

To specify number types, use suffixes in C#. Literal number suffixes are numeric suffixes.For example, for long type −// long suffix long val1 = 29345L;For double type −// double suffix double val2 = 297.325D; Console.WriteLine(val2);Let us see more examples −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       // long suffix       long val1 = 29345L;       Console.WriteLine(val1);       // decimal suffix       decimal val5 = 3245.5678M;       Console.WriteLine(val5);       // double suffix       double val2 = 297.325D;       Console.WriteLine(val2);       // float suffix       float val3 = 250.35F;       Console.WriteLine(val3);       // unsigned suffix       uint val4 = 3456U;       Console.WriteLine(val4);    } }Output29345 3245.5678 297.325 250.35 3456

Lowercase suffixes in C#

Samual Sam
Updated on 22-Jun-2020 14:42:40

100 Views

Set lowercase suffixes for literals such as u, l, ul, f, etc.// l for long long a = 29876l;It can be used on literal numbers as well. It tells the compiler that the literal is of a specific type.The following is an example −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       long a = 29876l;       float b = 95.10f;       Console.WriteLine(a);       Console.WriteLine(b);    } }On running the above example, you will get the following output. With that, you will also get a ... Read More

Get the creation time of a file in C#

karthikeya Boyini
Updated on 22-Jun-2020 14:43:02

2K+ Views

To get the creation time of a file in C#, use the CreationTime() method.For this, use the FileInfo as well as DateTime classes.Create an object of each −FileInfo file = new FileInfo("new.txt"); DateTime dt = file.CreationTime;Let us see the complete code −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       using (StreamWriter sw = new StreamWriter("qa.txt")) {          sw.WriteLine("Questions and Answers!");       }       FileInfo file = new FileInfo("qa.txt");       // file creation time       DateTime dt = file.CreationTime;       Console.WriteLine(dt);    } }Output9/5/2018 5:20:03 AM

Advertisements