Found 2587 Articles for Csharp

C# SingleorDefault() Method

Samual Sam
Updated on 23-Jun-2020 08:26:22

3K+ Views

The method returns a single specific element of a sequence. If the element is not present in the sequence, then the default value is returned.We have two string arrays here.string[] str1 = { "one" }; string[] str2 = { };First array is checked for a single element, whereas the second array is empty and checked using SingleorDefault.str2.AsQueryable().SingleOrDefault();The following is an example showing the usage of SingleorDefault() method.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       string[] str1 = { "one" };       string[] str2 = { }; ... Read More

C# Program to return the only element that satisfies a condition

Ankith Reddy
Updated on 23-Jun-2020 08:26:45

349 Views

The Single() method returns the only element that satisfies a condition. If more than one such element is visible, then an error is thrown.The following is our string array.string[] str = { "jack", "tom", "henry", "time"};Now, use the Single() method to get each element. Then, we have used Lambda Expression to calculate an element whose length is greater than four.str.AsQueryable().Single(name => name.Length > 4);Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       string[] str = { "jack", "tom", "henry", "time"};       // finding string whose length is ... Read More

C# Single() Method

karthikeya Boyini
Updated on 23-Jun-2020 08:27:09

2K+ Views

Get only a single element of a sequence using the Single() method.Let’s say we have a string array with only one element.string[] str = { "one" };Now, get the element.str.AsQueryable().Single();Here is our code.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       string[] str = { "one" };       string res = str.AsQueryable().Single();       Console.WriteLine(res);    } }Outputone

C# Program to read all the lines one by one in a file

Arjun Thakur
Updated on 23-Jun-2020 08:27:40

318 Views

Use ReadAllLines() method to read all the lines one by one in a file.Let’s say we have a file “new.txt” with the following lines.One Two ThreeFirstly, set the path of the file to be read.String myPath = "new.txt";Now add it under a string array to fetch the lines on by one.String[] fLine = File.ReadAllLines(myPath);Let’s say you need to fetch the first line. For that.fLine[0]The following is the complete example that reads lines one by one in a file.Exampleusing System; using System.IO; public class Demo {    public static void Main() {       String myPath = "new.txt";     ... Read More

LinkedList AddLast method in C#

Samual Sam
Updated on 23-Jun-2020 08:29:12

410 Views

Declare a string array.string [] students = {"Jenifer", "Angelina", "Vera"};Add it to a LinkedList.string [] students = {"Jenifer", "Angelina", "Vera"};Now, use AddLast() method to add a node at the end.list.AddLast("Anne");Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string [] students = {"Jenifer", "Angelina", "Vera"};       LinkedList list = new LinkedList(students);       foreach (var stu in list) {          Console.WriteLine(stu);       }       // add a node at the end       Console.WriteLine("Node added at the last...");       list.AddLast("Anne"); ... Read More

C# Queryable SequenceEqual() Method

Chandu yadav
Updated on 23-Jun-2020 08:29:40

124 Views

To check whether two sequences are equal or not, use the SequenceEqual() method.Firstly, set the sequences.Employee emp1 = new Employee { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 }; Employee emp2 = new Employee { EmployeeRank = 5, EmpName = "Raman", EmpMarks = 95 }; List employee1 = new List { emp1, emp2 }; List employee2 = new List { emp1, emp2 };Now, find whether the sequences are equal or not.employee1.AsQueryable().SequenceEqual(employee2);Here is the example that shows the result.Exmaple Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       Employee ... Read More

C# Linq SelectMany Method

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

928 Views

Use SelectMany method to collapse elements into a single collection like an error.An example would be to convert string to character array. The following is our string array.string[] str = { "Mobile", "Laptop", "Tablet" };Now, convert to character array.str.SelectMany(item => item.ToCharArray());Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       string[] str = { "Mobile", "Laptop", "Tablet" };       var res = str.SelectMany(item => item.ToCharArray());       Console.WriteLine("String converted to character array: ");       foreach (char letter in res) {          Console.Write(letter);       }    } }OutputString converted to character array: MobileLaptopTablet

C# Linq Select Method

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

2K+ Views

Use the Select method to modify the elements in an array.The following is our string array.string[] stationery = { "diary", "board", "pencil", "whiteboard" };The Select method also specifies Lambda Expressions as shown below −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       string[] stationery = { "diary", "board", "pencil", "whiteboard" };       var res = stationery.AsQueryable().Select((item, index) => new { result = item.Substring(0, index + 4) });       foreach (var str in res) {          Console.WriteLine("{0}", str);       }    } }Output{ result = diar } { result = board } { result = pencil } { result = whitebo }

C# Program to find the cube of elements in a list

Samual Sam
Updated on 23-Jun-2020 08:20:38

575 Views

Use Select method and Lambda Expression to calculate the cube of elements.The following is our list.List list = new List { 2, 4, 5, 7 };Now, use the Select() method and calculate the cube.list.AsQueryable().Select(c => c * c * c);The following is the entire example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       List list = new List { 2, 4, 5, 7 };       Console.WriteLine("Elements...");       // initial list javascript:void(0)       foreach (int n in list)       Console.WriteLine(n);     ... Read More

C# Console.WindowTop Property

Ankith Reddy
Updated on 23-Jun-2020 08:20:56

196 Views

The WindowsTop property is used to gets or set the top position of the console window area relative to the screen buffer.Declare an integer variable to get the top position.int top;Now, use the Console.WindowTop property.top = Console.WindowTop;Let us see the complete example.Example Live Demousing System; class Demo {    static void Main() {       int top;       top = Console.WindowTop;       Console.WriteLine("Top position of the Console window = "+top);    } }OutputTop position of the Console window = 0

Advertisements