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 on Trending Technologies
Technical articles with clear explanations and examples
DateTime.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 MoreMath.Sqrt() Method in C#
The Math.Sqrt() method in C# is used to compute the square root of a specified number. This static method is part of the System.Math class and returns a double value representing the positive square root of the input. Syntax Following is the syntax − public static double Sqrt(double val); Parameters val − A double-precision floating-point number for which to calculate the square root. Must be greater than or equal to zero for a valid numeric result. Return Value The method returns a double value with the following behavior − ...
Read MoreVariable Arguments (Varargs) in C#
The params keyword in C# allows a method to accept a variable number of arguments of the same type. This feature, known as variable arguments or varargs, enables you to call a method with any number of parameters without creating an array explicitly. Syntax Following is the syntax for declaring a method with variable arguments − public static returnType MethodName(params type[] parameterName) { // method body } The params keyword must be the last parameter in the method signature, and only one params parameter is allowed per method. Key Rules ...
Read MoreString slicing in C# to rotate a string
String slicing in C# allows you to rotate characters within a string by combining substrings. This technique uses the Substring() method to extract portions of a string and rearrange them to achieve the desired rotation effect. String rotation involves moving characters from one position to another within the string. For example, rotating the first two characters of "welcome" to the end results in "lcomewe". Syntax Following is the syntax for the Substring() method − string.Substring(startIndex, length) For string rotation, combine two substrings − string rotated = str.Substring(n) + str.Substring(0, n); ...
Read MoreString Formatting in C# to add padding
String formatting with padding in C# allows you to align text in columns by adding spaces to the left or right of strings. This is particularly useful for creating formatted tables, reports, or aligned output displays. Padding is controlled by format specifiers that define the width and alignment of each placeholder in the format string. Syntax The basic syntax for padding in format strings is − string.Format("{index, width}", value) Where − index − The parameter index (0, 1, 2, etc.) width − The total width of the field Positive width − ...
Read More