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
Server Side Programming Articles
Page 841 of 2109
Covariance and Contravariance in C#
Covariance and contravariance in C# enable flexible type relationships when working with generics, delegates, and interfaces. Covariance allows you to use a more derived type than originally specified, while contravariance allows you to use a more general type than originally specified. These concepts are essential for understanding how type safety works with generic interfaces and delegates, particularly when dealing with inheritance hierarchies. Class Hierarchy Example Let us consider the following class hierarchy where One is the base class, Two inherits from One, and Three inherits from Two − using System; class One { ...
Read MoreC# program to accept two integers and return the remainder
The remainder operation in C# finds the leftover value after dividing one integer by another. The modulus operator (%) is used to calculate the remainder when the first number is divided by the second number. Syntax Following is the syntax for calculating remainder using the modulus operator − int remainder = dividend % divisor; Where dividend is the number being divided and divisor is the number by which we divide. Using Basic Modulus Operation The simplest way to find the remainder is using the modulus operator directly − using System; ...
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 MoreWhat is the Keys property of Hashtable class in C#?
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 MoreC# Nested Classes
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 MoreC# program to generate secure random numbers
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 MoreC# Object Creation of Inherited Class
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 MoreC# Nullable Datetime
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 MoreDraw an ellipse in C#
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 MoreChaining comparison operators in C#
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