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
What are the different types of conditional statements supported by C#?
Conditional statements in C# allow programs to execute different code blocks based on specified conditions. These statements evaluate boolean expressions and direct program flow accordingly, enabling dynamic decision-making in applications. Types of Conditional Statements Statement Description if statement Executes a block of code when a boolean expression is true. if...else statement Executes one block if the condition is true, another if false. nested if statements Uses one if or else if statement inside another for complex conditions. switch statement Tests a variable against multiple values ...
Read MoreC# program to convert time from 12 hour to 24 hour format
Converting time from 12-hour format to 24-hour format in C# is a common requirement in applications. The DateTime.Parse() method can parse 12-hour time strings, and the ToString() method with the "HH:mm" format specifier converts it to 24-hour format. Syntax Following is the syntax for parsing 12-hour time and converting to 24-hour format − DateTime dateTime = DateTime.Parse("12-hour time string"); string time24Hour = dateTime.ToString("HH:mm"); The HH format specifier represents hours in 24-hour format (00-23), while mm represents minutes (00-59). Using DateTime.Parse() for Basic Conversion The simplest approach uses DateTime.Parse() to convert a 12-hour ...
Read MoreCollection Initialization in C#
Collection initialization in C# allows you to initialize collections with values at the time of creation using a concise syntax. This feature, introduced in C# 3.0, makes code more readable and eliminates the need for multiple Add() method calls. Collection initializers work with any collection type that implements IEnumerable and has an Add method, including List, Dictionary, arrays, and custom collections. Syntax Following is the syntax for collection initialization − CollectionType collectionName = new CollectionType { item1, item2, item3 }; For objects with properties, you can combine object and collection initialization − ...
Read MoreDecimal.ToOACurrency() Method in C#
The Decimal.ToOACurrency() method in C# converts a decimal value to an OLE Automation Currency value, which is represented as a 64-bit signed integer. OLE Automation Currency is a fixed-point data type that stores currency values with 4 decimal places of precision, making it suitable for financial calculations where precision is critical. The method multiplies the decimal value by 10, 000 to preserve 4 decimal places in the integer representation. This format is commonly used in COM interop scenarios and legacy systems that require OLE Automation compatibility. Syntax Following is the syntax for the Decimal.ToOACurrency() method − ...
Read MoreHow to use C# BinaryReader class?
The BinaryReader class in C# is used to read binary data from a stream in a specific encoding. It provides methods to read various data types like integers, doubles, strings, and byte arrays from binary files or streams. The BinaryReader class is located in the System.IO namespace and is commonly used for reading binary files, network streams, or any binary data format. Syntax Following is the syntax for creating a BinaryReader instance − BinaryReader reader = new BinaryReader(stream); Following is the syntax for reading different data types − int value = ...
Read MoreWhat is the difference between VAR and DYNAMIC keywords in C#?
The var and dynamic keywords in C# both allow you to declare variables without explicitly specifying their type, but they work very differently. The key difference is when type checking occurs — var is resolved at compile-time, while dynamic is resolved at runtime. Syntax Following is the syntax for declaring a var variable − var variableName = value; Following is the syntax for declaring a dynamic variable − dynamic variableName = value; Using var Keyword The var keyword creates statically typed variables. The compiler determines the type based on ...
Read MoreAction Delegate in C#
The Action delegate in C# is a built-in generic delegate type that represents a method with no return value (void). It can accept zero or more input parameters and is particularly useful for passing methods as parameters or storing method references. Syntax Following is the basic syntax for declaring an Action delegate − Action actionDelegate = MethodName; For Action delegates with parameters − Action actionDelegate = MethodName; Action actionDelegate = MethodName; Action actionDelegate = MethodName; Where T, T1, T2, etc. are the parameter types. Using Action Delegate with Single ...
Read MoreC# program to add two matrices
Matrix addition is a fundamental operation in linear algebra where two matrices of the same dimensions are added element by element. In C#, we can implement matrix addition using two-dimensional arrays. Syntax To declare a two-dimensional array for matrix operations − int[, ] matrix = new int[rows, columns]; Matrix addition formula for each element − result[i, j] = matrix1[i, j] + matrix2[i, j]; How Matrix Addition Works Matrix addition requires both matrices to have the same dimensions. Each element at position (i, j) in the first matrix is added ...
Read MoreDecimal.ToSByte() Method in C#
The Decimal.ToSByte() method in C# converts a decimal value to an equivalent 8-bit signed integer (sbyte). This method performs truncation, discarding any fractional part and returning only the integer portion of the decimal value. The sbyte data type can hold values from -128 to 127. If the decimal value is outside this range, an OverflowException will be thrown. Syntax Following is the syntax − public static sbyte ToSByte(decimal val); Parameters val − The decimal number to convert to an sbyte. Return Value Returns an sbyte value ...
Read MoreAccess Modifiers in C#
Access modifiers in C# control the visibility and accessibility of classes, methods, properties, and other members. They define which parts of your code can access specific members, providing encapsulation and security to your applications. Access Modifiers Overview C# provides five access modifiers that determine the scope and accessibility of class members − C# Access Modifiers Hierarchy public Most accessible protected internal internal protected private ...
Read More