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 803 of 2547
C# program to create a List with elements from an array
In C#, you can easily create a List with elements from an existing array. The List constructor accepts an IEnumerable parameter, which allows you to initialize the list directly with array elements. Syntax Following is the syntax to create a list from an array − List listName = new List(arrayName); You can also use the ToList() extension method − List listName = arrayName.ToList(); Using List Constructor The most straightforward way is to pass the array directly to the List constructor − using System; using System.Collections.Generic; public ...
Read MoreMath.Exp() Method in C#
The Math.Exp() method in C# returns e (Euler's number, approximately 2.718) raised to the specified power. This method is commonly used in mathematical calculations involving exponential functions, growth calculations, and scientific computations. Syntax Following is the syntax for the Math.Exp() method − public static double Exp(double d) Parameters d − A double-precision floating-point number representing the power to which e is raised. Return Value Returns a double value representing ed. Special cases include: If d equals Double.NaN, the method returns NaN. If ...
Read MoreWhat are identifiers in C#?
An identifier is a name used to identify a class, variable, function, or any other user-defined item in C#. Identifiers are fundamental building blocks that allow developers to name and reference different elements in their code. Rules for Naming Identifiers The basic rules for naming identifiers in C# are as follows − A name must begin with a letter or underscore that could be followed by a sequence of letters, digits (0-9), or underscores. The first character in an identifier cannot be a digit. It must not contain any embedded space or special ...
Read MoreFlow control in try catch finally in C#
The flow control in try-catch-finally blocks in C# follows a specific execution order depending on whether exceptions occur. Understanding this flow is crucial for proper error handling and resource management in your applications. Syntax Following is the syntax for try-catch-finally blocks − try { // Code that may throw an exception } catch (ExceptionType e) { // Exception handling code } finally { // Code that always executes } Flow Control Rules The try block executes first and contains code that might ...
Read MoreC# program to find the index of an element in a List
In C#, you can find the index of an element in a List using the IndexOf() method. This method returns the zero-based index of the first occurrence of the specified element, or -1 if the element is not found. Syntax Following is the syntax for the IndexOf() method − int IndexOf(T item) int IndexOf(T item, int startIndex) int IndexOf(T item, int startIndex, int count) Parameters item: The element to locate in the list. startIndex: The zero-based starting index of the search (optional). count: The number of elements to search (optional). ...
Read MoreC# Program to rename a file
In C#, you can rename a file using the File.Move() method from the System.IO namespace. This method moves a file from one location to another, and when the source and destination paths are in the same directory with different filenames, it effectively renames the file. Syntax Following is the syntax for renaming a file using File.Move() − File.Move(string sourceFileName, string destFileName); Parameters sourceFileName − The current path and name of the file to rename. destFileName − The new path and name for the file. Using File.Move() to Rename Files ...
Read MoreMath.Floor() Method in C#
The Math.Floor() method in C# returns the largest integral value that is less than or equal to the specified number. It rounds down to the nearest integer, which is particularly important to understand when working with negative numbers. Syntax The Math.Floor() method has two overloads − public static decimal Floor(decimal val); public static double Floor(double val); Parameters val − A decimal or double number to be rounded down to the nearest integer. Return Value Returns the largest integer less than or equal to val. The return type ...
Read MoreBoolean.CompareTo(Object) Method in C#
The Boolean.CompareTo(Object) method in C# compares the current Boolean instance to a specified object and returns an integer that indicates their relationship to one another. This method is useful for sorting Boolean values or implementing custom comparison logic. Syntax Following is the syntax − public int CompareTo(object obj); Where obj is an object to compare to this instance, or null. Return Value The method returns an integer value indicating the relationship between the current instance and the specified object − Less than zero: This instance is false and obj is ...
Read MoreDecimal.Multiply() Method in C#
The Decimal.Multiply() method in C# is used to multiply two specified decimal values and returns the result as a decimal. This method provides a safe way to perform decimal multiplication while maintaining precision and handling potential overflow conditions. Syntax Following is the syntax − public static decimal Multiply(decimal val1, decimal val2); Parameters val1 − The first decimal value (multiplicand). val2 − The second decimal value (multiplier). Return Value Returns a decimal value that represents the product of val1 and val2. Using Decimal.Multiply() for Basic ...
Read MoreHow to declare and initialize a dictionary in C#?
A Dictionary in C# is a collection that stores key-value pairs, where each key must be unique. It is part of the System.Collections.Generic namespace and provides fast lookups based on keys. Syntax Following is the syntax for declaring and initializing a Dictionary − Dictionary dictionaryName = new Dictionary(); Where TKey is the type of the key and TValue is the type of the value. You can also use the interface type for more flexibility − IDictionary dictionaryName = new Dictionary(); Using Dictionary.Add() Method The Add() method allows you ...
Read More