George John has Published 1167 Articles

ArgumentNullException in C#

George John

George John

Updated on 23-Jun-2020 08:49:16

626 Views

The exception thrown when a null reference is passed to a method that does not accept it as a valid argument.Let us see an example.When we set a null parameter to int.Parse() method, then ArgumentNullException is thrown as shown below −Exampleusing System; class Demo {    static void Main() { ... Read More

C# Average Method

George John

George John

Updated on 23-Jun-2020 08:43:44

18K+ Views

To find the average of integers in C#, use the Queryable Average() method.Let’s say the following is our integer array.var arr = new int[] { 10, 17, 25, 30, 40, 55, 60, 70 };Now, use the Average() method to get the average of the elements.double avg = Queryable.Average(arr.AsQueryable());Example Live Demousing System; ... Read More

LinkedList AddBefore method in C#

George John

George John

Updated on 23-Jun-2020 08:41:18

787 Views

Add a node before a given node in C# using the AddBefore() method.Our LinkedList with string nodes.string [] students = {"Henry", "David", "Tom"}; LinkedList list = new LinkedList(students);Now, let’s add node at the end.// adding a node at the end var newNode = list.AddLast("Brad");Use AddBefore() method to add a node ... Read More

C# Program to find the sum of a sequence

George John

George John

Updated on 23-Jun-2020 08:32:35

467 Views

Firstly, set a sequence.List myList = new List { 1, 2, 3, 4 ,5};Now find the sum using the Queryable Sum() method.myList.AsQueryable().Sum();Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       List myList = new List { 1, 2, 3, ... Read More

C# Linq Skip() Method

George John

George John

Updated on 23-Jun-2020 08:25:44

2K+ Views

Skip elements and return the remaining elements using the Skip() method.The following is an array.int[] marks = { 80, 55, 79, 99 };Now, let us skip 2 elements using Lambda Expressions, but this is done after arranging the elements in descending order.IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).Skip(2);Exampleusing System; using System.Linq; ... Read More

Represent Int32 as a Octal String in C#

George John

George John

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

911 Views

To represent Int32 as a Octal string in C#, use the ToString() method and set the base as the ToString() method’s second parameter i.e. 8 for Octal.Int32 represents a 32-bit signed integer.Firstly, set an Int32 variable.int val = 99;Now, convert it to octal string by including 8 as the second ... Read More

C# Linq Select Method

George John

George John

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

1K+ 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() { ... Read More

Get first three letters from every string in C#

George John

George John

Updated on 23-Jun-2020 08:19:11

2K+ Views

The following are our strings in a list −List list = new List { "keyboard", "mouse", "joystick", "monitor" };To use the first 3 letters, use substring method and use it under the Linq Select method.IEnumerable res = list.AsQueryable() .Cast() .Select(str => str.Substring(0, 3));Example Live Demousing System; using System.Linq; using System.Collections.Generic; class ... Read More

C# Program to find a value in a Hashtable

George John

George John

Updated on 23-Jun-2020 08:12:48

230 Views

Set Hahtable collection with elements.Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris");Let’s say now you need to find a value, then use the ContainsValue() method.We are finding value “Chris” here −h.ContainsValue(“Chris”);Example Live Demousing System; using System.Collections; public class Demo {    public static void Main() { ... Read More

Clear a Linked List in C#

George John

George John

Updated on 23-Jun-2020 08:05:42

136 Views

Clear a LinkedList using Clear() method.Let us first set a LinkedList.string [] employees = {"Patrick", "Robert", "John", "Jacob", "Jamie"}; LinkedList list = new LinkedList(employees);Now, let us clear the LinkedList.list.Clear();Let us see the complete code.Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string ... Read More

Advertisements