Csharp Articles

Page 166 of 196

C# program to add two matrices

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

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 More

What is the Keys property of Hashtable class in C#?

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 167 Views

The Keys property of the Hashtable class in C# returns an ICollection containing all the keys in the hashtable. This property is useful for iterating through all keys or performing operations on the key collection without accessing the values. Syntax Following is the syntax for accessing the Keys property − public virtual ICollection Keys { get; } Return Value The Keys property returns an ICollection object that contains all the keys in the hashtable. The order of keys is not guaranteed as hashtables do not maintain insertion order. Using Keys Property to ...

Read More

C# Nested Classes

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 2K+ Views

A nested class is a class declared inside another enclosing class. It is a member of its enclosing class, and nested classes can access private members of their enclosing class. However, the enclosing class cannot directly access private members of the nested class without creating an instance. Syntax Following is the syntax for declaring a nested class − public class OuterClass { // outer class members public class NestedClass { // nested class members } } ...

Read More

C# program to generate secure random numbers

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

For secure random numbers, use the RNGCryptoServiceProvider class or the newer RandomNumberGenerator class. These implement cryptographic random number generators that are suitable for security-sensitive applications like generating passwords, tokens, or encryption keys. Secure random numbers differ from regular Random class numbers because they use entropy from the operating system and are cryptographically secure, making them unpredictable even if an attacker knows some previously generated values. Syntax Following is the syntax for generating secure random bytes − using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider()) { byte[] randomBytes = new byte[4]; crypto.GetBytes(randomBytes); ...

Read More

C# Object Creation of Inherited Class

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

In C# inheritance, when creating objects of derived classes, the base class constructor is called first, followed by the derived class constructor. This ensures that the base class is properly initialized before the derived class adds its own functionality. The derived class inherits member variables and methods from the base class. When instantiating a derived class object, you can use the base keyword to explicitly call the base class constructor and pass required parameters. Syntax Following is the syntax for calling a base class constructor from a derived class − public class DerivedClass : BaseClass ...

Read More

C# Nullable Datetime

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

The nullable DateTime in C# allows you to assign null values to DateTime variables, which is useful when a date might not be available or applicable. This is particularly valuable in database operations where date fields can be NULL, or when dealing with optional date parameters. Syntax A nullable DateTime is declared using the question mark (?) syntax − DateTime? variableName = null; Alternatively, you can use the full Nullable syntax − Nullable variableName = null; Key Properties and Methods Nullable DateTime provides several useful properties and methods − ...

Read More

Draw an ellipse in C#

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

To draw an ellipse in C#, you use the DrawEllipse() method from the Graphics class. This method requires a Pen object to define the ellipse's outline and either a Rectangle or coordinate parameters to specify the ellipse's position and size. Drawing ellipses is commonly done in Windows Forms applications using the Paint event handler or by overriding the OnPaint method. Syntax The DrawEllipse() method has several overloads − graphics.DrawEllipse(pen, rectangle); graphics.DrawEllipse(pen, x, y, width, height); Parameters pen − Defines the color, width, and style of the ellipse outline rectangle − Specifies ...

Read More

Chaining comparison operators in C#

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 699 Views

C# supports chaining comparison operators based on operator associativity and precedence. When multiple operators of the same precedence appear in an expression, they are evaluated according to their associativity rules, typically left-to-right. Understanding how comparison operators chain together is crucial for writing correct conditional expressions and avoiding logical errors in your code. Syntax Following is the basic syntax for chaining comparison operators − variable1 == variable2 == variable3 variable1 != variable2 != variable3 The evaluation follows left-to-right associativity − (variable1 == variable2) == variable3 How Operator Chaining Works ...

Read More

Character constants vs String literals in C#

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

In C#, both character constants and string literals are used to represent text data, but they serve different purposes and have distinct syntax rules. Character constants represent single characters, while string literals represent sequences of characters (text). Character Constants Character constants are enclosed in single quotes and represent a single character. They are stored in variables of type char. Syntax char variableName = 'character'; Character constants can be − Plain characters − 'x', 'A', '5' Escape sequences − '', '\t', '' Unicode characters − '\u0041' (represents 'A') Example ...

Read More

What is the IsReadOnly property of SortedList class in C#?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 155 Views

The IsReadOnly property of the SortedList class in C# returns a boolean value indicating whether the collection is read-only. A read-only collection cannot be modified after creation − you cannot add, remove, or update elements. For standard SortedList instances created using the default constructor, this property always returns false, meaning the collection is modifiable. Syntax Following is the syntax for using the IsReadOnly property − bool isReadOnly = sortedList.IsReadOnly; Return Value The IsReadOnly property returns − true − if the SortedList is read-only false − if the SortedList can be ...

Read More
Showing 1651–1660 of 1,951 articles
« Prev 1 164 165 166 167 168 196 Next »
Advertisements