Chandu yadav has Published 1163 Articles

How to handle empty collections in C#

Chandu yadav

Chandu yadav

Updated on 23-Jun-2020 08:08:52

334 Views

To handle empty collections, use the DefaultIfEmpty() method in C#.If an array is empty, then using this method will show the default method instead of displaying an error.Let’s say we have an empty list.List myList = new List();Now, use DefaultIfEmpty() method to display the default value.myList.DefaultIfEmpty();Example Live Demousing System; using System.Linq; ... Read More

How to negate the positive elements of an integer array in C#?

Chandu yadav

Chandu yadav

Updated on 23-Jun-2020 08:00:23

293 Views

The following is the array and its elements −int[] arr = { 10, 20, 15 };Set negative value to positive elements.if (arr[i] > 0) arr[i] = -arr[i];Loop the above until the length of the array.for (int i = 0; i < arr.Length; i++) {    Console.WriteLine(arr[i]);    if (arr[i] > ... Read More

C# Program to find the longest string from an array of strings using Lambda Expression

Chandu yadav

Chandu yadav

Updated on 23-Jun-2020 07:56:57

729 Views

The following is our string array −string[] arr = { "Java", "HTML", "CSS", "JavaScript"};Use the Aggregate method and set a Lambda Expression to find the string with more number of characters.Here, the resultant string should have more number of characters than the initial seed value i.e. “jQuery” here.Example Live Demousing System; ... Read More

Implicit conversion from 8-bit signed integer (SByte) to Decimal in C#

Chandu yadav

Chandu yadav

Updated on 23-Jun-2020 07:50:23

167 Views

SByte represents an 8-bit signed integer.To implicitly convert an 8-bit signed integer to a Decimal, firstly set an sbyte value.sbyte val = 51;To convert sbyte to decimal, assign the value.decimal d; d = val;Let us see another example.Exampleusing System; public class Demo {    public static void Main() {   ... Read More

LinkedList AddAfter method in C#

Chandu yadav

Chandu yadav

Updated on 23-Jun-2020 07:48:28

1K+ Views

Set a LinkedList.int [] num = {1, 2, 3, 4, 5}; LinkedList list = new LinkedList(num);Now add a node at the end using AddLast() method.var newNode = list.AddLast(20);To add a node after the above added node, use the AddAfter() method.list.AddAfter(newNode, 30);Example Live Demousing System; using System.Collections.Generic; class Demo {    static ... Read More

C# Program to return a collection with repeated elements

Chandu yadav

Chandu yadav

Updated on 23-Jun-2020 07:44:12

234 Views

To return a collection with repeated elements in C#, use Enumerable.Repeat method.It is part of System.Linq namespace.Let’s say you need to repeat a number twice, for that set the number and the frequency of repetition.Enumerable.Repeat(50, 2);Now assign it to a variable and display it.Example Live Demousing System; using System.Linq; class Demo ... Read More

C# Console BufferHeight Property

Chandu yadav

Chandu yadav

Updated on 23-Jun-2020 07:38:38

91 Views

Use the BufferHeight gets or sets the height of the buffer area.Use the property like this −Console.BufferHeightLet us see the complete example.Example Live Demousing System; class Demo {    static void Main() {       Console.WriteLine("Buffer height (rows) = "+Console.BufferHeight);    } }OutputBuffer height (rows) = 0

Convert.ToChar Method in C#

Chandu yadav

Chandu yadav

Updated on 23-Jun-2020 07:35:47

926 Views

The Convert.ToChar method is used to convert a specified value to a Unicode integer.We have declared an sbyte variable.sbyte byteVal = 200;Now, use the Convert.ToChar() method to convert the sbyte value to a Unicode integer.charVal = Convert.ToChar(b);Let us see another example.Example Live Demousing System; public class Demo {    public static ... Read More

C# DateTime Max Value

Chandu yadav

Chandu yadav

Updated on 23-Jun-2020 07:29:21

4K+ Views

To set the max value for a Date, use the DateTime property MaxValue.DateTime max = DateTime.MaxValue;Now, display the value of max to get the maximum value of a date as shown below.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       DateTime ... Read More

C# Program to add a node after the given node in a Linked List

Chandu yadav

Chandu yadav

Updated on 23-Jun-2020 07:24:57

210 Views

Set a LinkedList and add elements.string [] students = {"Beth", "Jennifer", "Amy", "Vera"}; LinkedList list = new LinkedList(students);Firstly, add a new node at the end.var newNode = list.AddLast("Emma");Now, use the AddAfter() method to add a node after the given node.list.AddAfter(newNode, "Matt");The following is the complete code.Example Live Demousing System; using System.Collections.Generic; ... Read More

Advertisements