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
Programming Articles
Page 875 of 2547
How to declare a two-dimensional array in C#
A two-dimensional array in C# is a data structure that stores elements in a grid-like format with rows and columns. It can be thought of as an array of arrays, where each element is accessed using two indices. Syntax Following is the syntax for declaring a two-dimensional array − datatype[, ] arrayName; Following is the syntax for initializing a two-dimensional array − datatype[, ] arrayName = new datatype[rows, columns]; You can also declare and initialize in one statement − datatype[, ] arrayName = new datatype[rows, columns] { ...
Read MoreHow to print multiple blank lines in C#?
In C#, there are several ways to print multiple blank lines to the console. You can use loops, string repetition, or direct method calls depending on your needs. Using a While Loop The most straightforward approach is to use a while loop to print blank lines repeatedly − using System; namespace Program { public class Demo { public static void Main(string[] args) { int a = 0; while ...
Read MoreWhy is f required while declaring floats in C#?
The f suffix is required when declaring float literals in C# because without it, the compiler treats decimal numbers as double by default. The f suffix explicitly tells the compiler that the literal should be treated as a float type. Why the 'f' Suffix is Needed In C#, numeric literals with decimal points are interpreted as double precision floating-point numbers by default. Since double has higher precision than float, implicit conversion from double to float is not allowed because it could result in data loss. Literal Type Assignment ...
Read MoreDifference between Static Constructor and Instance Constructor in C#
In C#, constructors are special methods used to initialize objects. There are two main types: static constructors and instance constructors. Understanding their differences is crucial for proper class initialization and memory management. Static Constructor A static constructor is declared using the static modifier and is responsible for initializing static members of a class. It executes only once during the application lifetime, before any static members are accessed or any instances are created. Syntax static ClassName() { // initialize static members } Instance Constructor An instance constructor initializes instance ...
Read MoreDateTime.AddYears() Method in C#
The DateTime.AddYears() method in C# adds a specified number of years to a DateTime instance and returns a new DateTime object. This method is particularly useful for calculating future or past dates, handling anniversaries, or working with year-based intervals. Syntax Following is the syntax for the AddYears() method − public DateTime AddYears(int value); Parameters The method accepts a single parameter − value − An integer representing the number of years to add. Use positive values to add years and negative values to subtract years. Return Value Returns a ...
Read MoreWhat does the keyword var do in C#?
The var keyword in C# enables implicit type declaration where the compiler automatically determines the variable's type based on the assigned value. This feature was introduced in C# 3.0 and provides cleaner code while maintaining type safety. Syntax Following is the syntax for using var keyword − var variableName = initialValue; The compiler infers the type from the initial value. Once declared, the variable behaves as if it were declared with its actual type. How It Works When you use var, the C# compiler performs type inference at compile time. The variable ...
Read MoreHow to create a Directory using C#?
To create, move, and delete directories in C#, the System.IO.Directory class provides essential methods for directory operations. The Directory.CreateDirectory() method is used to create new directories at specified paths. Syntax Following is the syntax for creating a directory using Directory.CreateDirectory() method − Directory.CreateDirectory(string path); Following is the syntax for checking if a directory exists before creating it − if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } Parameters path − A string representing the directory path to create. Can be an absolute or relative path. ...
Read MoreC# program to get max occurred character in a String
To find the maximum occurred character in a string in C#, we need to count the frequency of each character and then identify which character appears most frequently. This can be achieved using different approaches including character arrays and dictionaries. Syntax Using a character frequency array − int[] frequency = new int[256]; // ASCII characters for (int i = 0; i < str.Length; i++) frequency[str[i]]++; Using a dictionary for character counting − Dictionary charCount = new Dictionary(); foreach (char c in str) { charCount[c] ...
Read MoreJoin, Sleep and Abort methods in C# Threading
The Thread class in C# provides several important methods to control thread execution: Join(), Sleep(), and Abort(). These methods allow you to synchronize threads, pause execution, and terminate threads when needed. Syntax Following is the syntax for the three main thread control methods − // Join method thread.Join(); thread.Join(timeout); // Sleep method Thread.Sleep(milliseconds); // Abort method (obsolete in .NET Core/.NET 5+) thread.Abort(); Join Method The Join() method blocks the calling thread until the target thread terminates. This ensures that the calling thread waits for another thread to complete before continuing execution. ...
Read MoreDelegation vs Inheritance in C#
In C#, both delegation and inheritance are fundamental concepts that enable code reusability and polymorphism, but they work in fundamentally different ways. Delegation uses composition and method references, while inheritance establishes an "is-a" relationship between classes. Delegation in C# A delegate is a reference type variable that holds references to methods. It enables runtime flexibility by allowing you to change method references dynamically. Delegation follows the composition principle where objects contain references to other objects. Syntax delegate Example using System; public delegate void NotificationHandler(string message); public ...
Read More