Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by George John
Page 4 of 79
Difference between Method and Function in C#
In C#, the terms method and function are often used interchangeably, but there is a subtle distinction. A method is a function that belongs to a class or struct, while a function is a more general term for a reusable block of code that performs a specific task. Every C# program has at least one class with a method named Main. Methods are defined within classes and operate on the data and behavior of those classes. Syntax Following is the syntax for defining a method in C# − [access modifier] [return type] MethodName(parameters) { ...
Read MoreHow to control for loop using break and continue statements in C#?
The break and continue statements provide powerful control flow mechanisms within for loops in C#. The break statement terminates the loop entirely, while the continue statement skips the current iteration and moves to the next one. Syntax Following is the syntax for using break statement in a for loop − for (initialization; condition; increment) { if (someCondition) { break; // exits the loop completely } // other statements } Following is the syntax for using continue statement in a ...
Read MoreC# program to create an empty string array
Creating an empty string array in C# is useful when you need to declare an array without initial elements. There are several ways to create an empty string array, each serving different purposes depending on your requirements. Syntax Following are the different ways to create an empty string array − string[] str = new string[] {}; string[] str = new string[0]; string[] str = {}; Using Array Initializer Syntax The most straightforward way to create an empty string array is using the array initializer syntax with empty braces − using System; ...
Read MoreHow are values assigned to arrays in C#?
Declaring an array does not initialize the array in memory. When the array variable is initialized, you can assign values to the array using several different approaches in C#. Array is a reference type, so you need to use the new keyword to create an instance of the array. There are multiple ways to assign values to arrays, each with its own syntax and use cases. Syntax Following is the syntax for declaring and initializing arrays − // Declaration with size datatype[] arrayName = new datatype[size]; // Assignment using index arrayName[index] = value; ...
Read MoreBool Array in C#
A bool array in C# is used to store multiple boolean values (true and false) in a single collection. Boolean arrays are useful for tracking states, flags, or conditions across multiple elements. Syntax Following are the different ways to declare and initialize a bool array − // Declaration with size bool[] arrayName = new bool[size]; // Declaration with initialization bool[] arrayName = {true, false, true}; // Using new keyword with values bool[] arrayName = new bool[] {true, false, true}; Creating and Initializing Bool Arrays Method 1: Declaration with Size and Assignment ...
Read MoreHow to use ReadKey() method of Console class in C#?
The Console.ReadKey() method in C# reads the next character or function key pressed by the user. This method is commonly used to pause program execution until the user presses a key, which is particularly useful when running console applications from Visual Studio to prevent the console window from closing immediately. Syntax The Console.ReadKey() method has two overloads − public static ConsoleKeyInfo ReadKey(); public static ConsoleKeyInfo ReadKey(bool intercept); Parameters intercept (optional): A boolean value that determines whether to display the pressed key in the console. If true, the key is not displayed; if ...
Read Morefinal, finally and finalize in C#
In C#, the terms final, finally, and finalize serve different purposes. While final doesn't exist in C#, finally and finalize are crucial concepts for exception handling and resource cleanup respectively. final (sealed in C#) C# does not have a final keyword like Java. Instead, C# uses the sealed keyword to achieve similar functionality − public sealed class SealedClass { // cannot be inherited } public override sealed void SealedMethod() { // cannot be overridden further } The sealed keyword prevents overriding of methods or inheritance of classes. ...
Read MoreThe nameof keyword in C#
The nameof operator in C# returns the string literal name of a variable, type, or member. It provides a compile-time constant string that represents the name of the code element, making it useful for logging, exception messages, and property change notifications without hardcoding strings. Syntax Following is the syntax for using the nameof operator − string name = nameof(element); Where element can be a variable, property, method, class, or namespace. Using nameof with Variables The nameof operator returns the variable name as a string, which is resolved at compile time − ...
Read MoreHashSet in C#
A HashSet in C# is a high-performance collection that stores unique elements and automatically eliminates duplicates. It provides fast lookups, insertions, and deletions with O(1) average time complexity, making it ideal for scenarios where you need to maintain a collection of distinct values. HashSet is part of the System.Collections.Generic namespace and implements the ISet interface, providing mathematical set operations like union, intersection, and difference. Syntax Following is the syntax for declaring and initializing a HashSet − HashSet hashSetName = new HashSet(); You can also initialize it with an existing collection − ...
Read MoreCombine two arrays in C#
Combining two arrays in C# can be accomplished using various methods. The most common approaches include using List.AddRange() method, Array.Copy(), or LINQ's Concat() method. Syntax Using List.AddRange() method − var list = new List(); list.AddRange(array1); list.AddRange(array2); T[] combinedArray = list.ToArray(); Using Array.Copy() method − T[] combinedArray = new T[array1.Length + array2.Length]; Array.Copy(array1, 0, combinedArray, 0, array1.Length); Array.Copy(array2, 0, combinedArray, array1.Length, array2.Length); Using LINQ Concat() method − T[] combinedArray = array1.Concat(array2).ToArray(); Using List.AddRange() Method This approach creates a List, adds both arrays using AddRange(), then converts ...
Read More