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 804 of 2547
Optimization Tips for C# Code
C# code optimization involves writing efficient code that executes faster and uses memory more effectively. Here are essential optimization tips that can significantly improve your application's performance. Prefer Generic Collections Over Non-Generic Use List instead of ArrayList whenever possible. Generic collections provide type safety and better performance by avoiding boxing and unboxing operations − using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; class Program { public static void Main() { // Inefficient - ArrayList with boxing ArrayList arrayList = new ...
Read MoreHow to convert Decimal to Binary using C#?
Converting decimal numbers to binary in C# can be accomplished using the division and modulus operators. This process involves repeatedly dividing the decimal number by 2 and collecting the remainders, which form the binary representation when reversed. The algorithm works by dividing the decimal value by 2, storing the remainder (which is always 0 or 1), and continuing until the quotient becomes 0. The binary representation is formed by reading the remainders in reverse order. Algorithm The conversion process follows these steps − Divide the decimal number by 2 Store the remainder ...
Read MoreC# Program to find the smallest element from an array using Lambda Expressions
Lambda expressions in C# provide a concise way to write anonymous functions. When combined with LINQ methods like Min(), they offer powerful tools for array operations. This article demonstrates how to find the smallest element from an array using lambda expressions. Syntax The basic syntax for using Min() with lambda expressions − array.Min() // finds minimum value directly array.Min(element => expression) // finds minimum based on expression Using Min() Without Lambda Expression For ...
Read MoreImplicit conversion from 8-bit signed integer (SByte) to Decimal in C#
SByte represents an 8-bit signed integer with values ranging from -128 to 127. C# allows implicit conversion from sbyte to decimal because this conversion is always safe and does not result in data loss. Syntax Following is the syntax for implicit conversion from sbyte to decimal − sbyte sbyteValue = value; decimal decimalValue = sbyteValue; // implicit conversion How It Works The conversion happens automatically without requiring explicit casting because decimal can represent all possible sbyte values without precision loss. The 8-bit signed integer is widened to fit into the 128-bit decimal ...
Read MoreWhat are abstract classes in C#?
An abstract class in C# is a class that cannot be instantiated directly and contains one or more abstract methods that must be implemented by derived classes. Abstract classes provide a common base with shared functionality while requiring derived classes to implement specific methods with their own specialized behavior. Abstract classes are declared using the abstract keyword and serve as blueprints for related classes that share common characteristics but need different implementations for certain operations. Syntax Following is the syntax for declaring an abstract class with abstract methods − abstract class ClassName { ...
Read MoreC# program to multiply two matrices
Matrix multiplication is a mathematical operation that combines two matrices to produce a third matrix. This operation is only possible when the number of columns in the first matrix equals the number of rows in the second matrix. If matrix A has dimensions m×n and matrix B has dimensions n×p, the resulting matrix C will have dimensions m×p. Matrix Multiplication Rule Matrix A m × n 2 × 3 × Matrix B n × p ...
Read MoreDecimal.Negate() Method in C#
The Decimal.Negate() method in C# is used to return the result of multiplying the specified decimal value by negative one. This method effectively changes the sign of a decimal number − positive numbers become negative and negative numbers become positive. Syntax Following is the syntax − public static decimal Negate(decimal val); Parameters val − The decimal value to negate. Return Value Returns a decimal number that is equal to val multiplied by negative one (-1). If val is positive, the result is negative. If val is negative, the result ...
Read MoreWhat are dynamic data types in C#?
The dynamic data type in C# allows you to store any type of value in a variable. Unlike other data types, type checking for dynamic variables occurs at runtime rather than compile-time, providing flexibility but requiring careful handling to avoid runtime errors. Syntax Following is the syntax for declaring a dynamic variable − dynamic = value; Basic Dynamic Variable Declaration Example using System; class Program { public static void Main() { dynamic val1 = 100; ...
Read MorePrivate Variables in C#
The private access specifier in C# allows a class to hide its member variables and member functions from other functions and objects. Only methods of the same class can access its private members. Even an instance of a class cannot access its private members directly from outside the class. Syntax Following is the syntax for declaring a private variable − private dataType variableName; For example − private double length; private int count; private string name; Key Rules of Private Variables Private members can only be accessed within ...
Read MoreHow to check if String is Palindrome using C#?
A palindrome is a string that reads the same forwards and backwards, such as "Level", "madam", or "racecar". In C#, there are several ways to check if a string is a palindrome. Using Array.Reverse() Method The simplest approach is to reverse the string and compare it with the original. First, convert the string to a character array − char[] ch = str.ToCharArray(); Then reverse the array using Array.Reverse() − Array.Reverse(ch); Finally, compare the reversed string with the original using case-insensitive comparison − bool res = str.Equals(rev, StringComparison.OrdinalIgnoreCase); ...
Read More