Category: LINQ

How to Get the Top 5 elements from a List in C# ?

If you want to get the top 5 elements from a list in C# or skip the first item and get the next five elements from a List , you can use the Take and the Skip methods. How to Get the Top 5 elements from a List in C# ? Below is a sample code snippet demonstrating the usage of the Skip and Take…

Example of Anonymous methods in C#

Anonymous methods in C# are like regular methods nut does not contain any name. Anonymous methods are just like regular methods  and uses the delegate keyword and does not need a name , parameter or return type. Example of Anonymous methods in C# Below is a sample code snippet demonstrating the usage of the anonymous types in C# using System; using System.Collections.Generic; namespace AbundantCodeConsoleApp {…

Example of Anonymous Type Indexes in For Loop in C#

The anonymous type (var) can be used to initialize the indexes in a for loop or the object within the for each loop. Below is a sample code snippet demonstrating the usage of the anonymous types in the For loop . Example of Anonymous Type Indexes in For Loop in C# using System; namespace AbundantCodeConsoleApp { class Program { static void Main(string[] args) { var…

Anonymous Types are read-only in C#

If you try to set a value to the anonymous type in C# , you will receive an compiler error . The properties within the anonymous types are read-only properties. Anonymous Types are read-only in C# using System; namespace AbundantCodeConsoleApp { class Program { static void Main(string[] args) { var employee = new { FirstName = “Abundant”, Last = “Code.com” }; employee.FirstName = “Lot of…

Example of Composite Anonymous Types in C#

The feature of anonymous types can be felt specially when we work with the composite type rather than the simple types. The below example is more like you are defining an inline class without actually having the definition. Example of Composite Anonymous Types in C# Below is an example of the anonymous type in C# that demonstrates the composite anonymous type. using System; namespace AbundantCodeConsoleApp…

Anonymous Type Initialized with Array of Integers

using System; namespace AbundantCodeConsoleApp { class Program { static void Main(string[] args) { var numbers = new int[] { 1, 1, 2, 3, 4, 8, 33, 2 }; Console.WriteLine(numbers[0]); Console.ReadLine(); } } } Tutorials you might find interesting:C Program to generate random numbersC Program to sort numbers in ascending orderC program to compare 2 numbers using ternary operatorC Program to sort the numbers in descending…

Defining Simple Anoymous Type in C#

To define an anonymous type in C# , we can use the var keyword . The syntax for defining the anonymous type in C# is as follows var <variableName> = <Non-NULL Value> Eg: var WebsiteName = “Abundantcode.com”; In the MSIL , the above code is similar to string WebsiteName = “Abundantcode.com”; Tutorials you might find interesting:Senthil-Kumar-B-Posted-by-Senthil-Kumar-B-August-12-2020-CSharp-3-Min-Read-Microsoft-had-released-the-preview-version-of-.NET-5-some-time-back-which-comes-with-C-9-Preview-too.-While-it-is-sti…Azure Q&A – Part 1C Program to find the simple…

Rules for using Anonymous Types in C#

The anonymous types (var) in C# is an interesting feature that the C# developers could use within their .NET application . When using the anonymous type , there are certain criteria or rules that needs to be followed . Rules for using Anonymous Types in C# Below are some of the rules that the developers should be following when using the anonymous type. 1. NULL…

Example of Lambda Expression with Action method with String as Parameter in C#

Below is a sample code snippet that demonstrates the lambda expression with action method with string as parameter in C#. Example of Lambda Expression with Action method with String as Parameter using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ACSample { class Program { static void Main(string[] args) { // Example of Lambda Expression with Action method with String as Parameter Action<string>…

Example of Lambda Expression with Action Method in C#

Below is a sample code snippet that demonstrates the lambda expression with action method in C#. Example of Lambda Expression with Action Method in C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ACSample { class Program { static void Main(string[] args) { // Example of Lambda Expression with Action method Action Lambda = () => Console.WriteLine(“Hello World”); Lambda(); Console.ReadLine(); } }…