Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 24 of 151

How to input multiple values from user in one line in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 3K+ Views

In C#, you can input multiple values from the user in one line using several approaches. The most common method is using Console.ReadLine() with Split() to parse space-separated or comma-separated values into an array. Syntax Following is the syntax for reading multiple values in one line using Split() − string input = Console.ReadLine(); string[] values = input.Split(' '); // or Split(', ') for comma-separated For converting to integers − int[] numbers = input.Split(' ').Select(int.Parse).ToArray(); Using Split() with Space Separator This approach reads a single line of space-separated values and ...

Read More

C# program to check whether two sequences are the same or not

Samual Sam
Samual Sam
Updated on 17-Mar-2026 238 Views

The SequenceEqual method in C# is a LINQ extension method that determines whether two sequences are equal by comparing their elements using the default equality comparer. It returns true if the sequences have the same length and corresponding elements are equal, otherwise it returns false. Syntax Following is the syntax for using SequenceEqual method − bool result = sequence1.SequenceEqual(sequence2); You can also use a custom equality comparer − bool result = sequence1.SequenceEqual(sequence2, comparer); Parameters sequence2 − The sequence to compare with the current sequence. comparer ...

Read More

How to instantiate a class in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 5K+ Views

In C#, you create an instance of a class using the new operator. This process is called instantiation, which allocates memory for the object and calls the class constructor. Syntax Following is the basic syntax for instantiating a class − ClassName objectName = new ClassName(); For classes with parameterized constructors − ClassName objectName = new ClassName(parameters); Using Default Constructor When you instantiate a class without parameters, it calls the default constructor. Here's an example using a Line class − using System; class Line { ...

Read More

C# Cast method

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

The Cast() method in C# is a LINQ extension method used to cast each element in a collection from one type to another. It is particularly useful when working with collections of object type that need to be converted to a specific type for further processing. Syntax Following is the syntax for the Cast() method − public static IEnumerable Cast(this IEnumerable source) Parameters source − The collection containing elements to be cast. TResult − The target type to cast elements to. Return Value Returns an IEnumerable containing each element ...

Read More

How to iterate efficiently through an array of integers of unknown size in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 587 Views

To iterate efficiently through an array of integers of unknown size in C#, there are several approaches available. The key is to use the array's Length property or built-in iteration methods that automatically handle the array size. Syntax Following is the syntax for basic array iteration using a for loop − for (int i = 0; i < arr.Length; i++) { // access arr[i] } Following is the syntax for using foreach loop − foreach (int element in arr) { // access element directly ...

Read More

What are tokens in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 3K+ Views

A token is the smallest element of a C# program that the compiler can recognize. Tokens are the building blocks of C# code and include keywords, identifiers, literals, operators, and punctuation marks. Understanding tokens is fundamental to writing valid C# programs. Types of Tokens in C# Keywords int, if, class Reserved words Identifiers myVar, Main Names Literals 42, "hello" Values Operators ...

Read More

How to join or concatenate two lists in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 3K+ Views

In C#, there are multiple ways to join or concatenate two lists. The most common approaches include using the AddRange() method, LINQ's Concat() method, and the Union() method. Each method has different behaviors and use cases. Syntax Following is the syntax for using AddRange() to concatenate lists − list1.AddRange(list2); Following is the syntax for using LINQ Concat() method − var result = list1.Concat(list2).ToList(); Following is the syntax for using Union() to join lists without duplicates − var result = list1.Union(list2).ToList(); Using AddRange() Method The AddRange() ...

Read More

C# DefaultIfEmpty Method

Samual Sam
Samual Sam
Updated on 17-Mar-2026 345 Views

The DefaultIfEmpty method in C# is a LINQ extension method used to handle empty collections gracefully. Instead of returning an empty sequence that might cause issues in further operations, it returns a sequence containing a single default value when the original collection is empty. Syntax Following is the syntax for the DefaultIfEmpty method − public static IEnumerable DefaultIfEmpty( this IEnumerable source ) public static IEnumerable DefaultIfEmpty( this IEnumerable source, TSource defaultValue ) Parameters source − The sequence to return ...

Read More

Decimal type in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

The decimal type in C# is a 128-bit data type designed for financial and monetary calculations that require high precision. Unlike float and double, the decimal type provides exact decimal representation without rounding errors, making it ideal for applications involving currency and precise arithmetic operations. Syntax Following is the syntax for declaring decimal variables − decimal variableName = value; decimal variableName = valueM; // M or m suffix required for literals The M or m suffix is required when assigning decimal literals to distinguish them from double values − decimal price = ...

Read More

How to loop through all values of an enum in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 3K+ Views

In C#, you can loop through all values of an enum using the Enum.GetValues() method. This method returns an array containing all the values defined in the enum, which you can then iterate over using a foreach loop. Syntax Following is the syntax for looping through enum values − foreach (EnumType value in Enum.GetValues(typeof(EnumType))) { // Process each enum value } You can also use the generic version for type safety − foreach (EnumType value in Enum.GetValues()) { // Process each enum value (C# ...

Read More
Showing 231–240 of 1,507 articles
« Prev 1 22 23 24 25 26 151 Next »
Advertisements