Csharp Articles

Page 88 of 196

How to concatenate Two Arrays in C#?

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

To concatenate two arrays in C#, you can use several methods depending on your requirements. The most common approaches include using the Concat() method from LINQ, copying elements manually, or using Array.Copy(). Syntax Using LINQ Concat() method − var result = array1.Concat(array2).ToArray(); Using Array.Copy() method − Array.Copy(sourceArray, destinationArray, length); Using LINQ Concat() Method The Concat() method from LINQ is the most straightforward way to concatenate arrays. It creates a new array containing elements from both arrays − using System; using System.Linq; class Program { ...

Read More

What are control statements in C#?

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

Control statements in C# determine the flow of program execution by specifying which code blocks to execute based on conditions or how many times to repeat certain operations. These statements are fundamental building blocks that allow developers to create dynamic and responsive applications. C# provides several types of control statements that can be categorized into conditional statements (if, if-else, switch) and loop statements (for, while, do-while, foreach). Let's explore the main control statements with practical examples. if Statement An if statement executes a block of code only when a specified boolean condition evaluates to true. Syntax ...

Read More

What are destructors in C# programs?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 2K+ Views

A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope or is garbage collected. It performs cleanup operations before the object is destroyed from memory. A destructor has exactly the same name as the class with a prefixed tilde (~), and it cannot have parameters or return values. It is automatically called by the garbage collector, not directly by the programmer. Syntax Following is the syntax for declaring a destructor − ~ClassName() { // cleanup code } ...

Read More

What are different methods of passing parameters in C#?

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

When a method with parameters is called, you need to pass the parameters to the method using one of the following three methods in C# − Value Parameters (Default) This method copies the actual value of an argument into the formal parameter of the function. Changes made to the parameter inside the function have no effect on the original argument because the method works with a copy of the value. Value Parameters - Copy by Value Main Method int a = 7 int b ...

Read More

Iterators in C#

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

An iterator in C# performs custom iteration over a collection using the yield keyword. It returns elements one at a time and remembers its current position, making it memory-efficient for large datasets. Iterators implement lazy evaluation, meaning values are generated only when requested. Syntax Following is the syntax for creating an iterator method − public static IEnumerable MethodName() { // logic here yield return value; // more logic yield return anotherValue; } The yield break statement can be ...

Read More

How to write the first program in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 638 Views

Writing your first program in C# is the foundation of learning this powerful programming language. A C# program consists of several key components that work together to produce output. Let's explore the structure and elements of a basic C# program. Syntax Following is the basic structure of a C# program − using System; namespace NamespaceName { class ClassName { static void Main(string[] args) { // Program logic here ...

Read More

How to declare and initialize a dictionary in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 2K+ Views

A Dictionary in C# is a collection that stores key-value pairs, where each key must be unique. It is part of the System.Collections.Generic namespace and provides fast lookups based on keys. Syntax Following is the syntax for declaring and initializing a Dictionary − Dictionary dictionaryName = new Dictionary(); Where TKey is the type of the key and TValue is the type of the value. You can also use the interface type for more flexibility − IDictionary dictionaryName = new Dictionary(); Using Dictionary.Add() Method The Add() method allows you ...

Read More

How to declare and initialize a list in C#?

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

A List in C# is a generic collection that stores elements in a resizable array. You can declare and initialize a List in several ways, depending on whether you want to add elements immediately or start with an empty collection. Syntax Following is the basic syntax for declaring a List − List listName = new List(); Following is the syntax for initializing a List with values − List listName = new List() { value1, value2, value3 }; Empty List Declaration ...

Read More

How to declare and initialize constant strings in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 11K+ Views

To declare a constant string in C#, use the const keyword. Constants are immutable values that are set at compile-time and cannot be changed during program execution. Once initialized, attempting to modify a constant will result in a compile-time error. Syntax Following is the syntax for declaring and initializing a constant string − const string constantName = "value"; Constants must be initialized at the time of declaration and the value must be a compile-time constant expression. Key Rules for Constants Constants must be initialized at declaration − you cannot declare ...

Read More

How to declare and instantiate Delegates in C#?

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

C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime, making delegates powerful tools for implementing callback methods and event handling. Syntax Following is the syntax for declaring delegates − delegate Following is the syntax for instantiating delegates − delegateInstance = new (MethodName); Delegate Declaration and Instantiation 1. Declare Delegate delegate int ...

Read More
Showing 871–880 of 1,951 articles
« Prev 1 86 87 88 89 90 196 Next »
Advertisements