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 by Samual Sam
Page 14 of 151
C# Program to remove a node at the beginning of a Linked List
To remove a node at the beginning of a LinkedList in C#, use the RemoveFirst() method. This method removes the first node from the LinkedList and automatically adjusts the internal structure. Syntax Following is the syntax for the RemoveFirst() method − linkedList.RemoveFirst(); This method removes the first node and does not return any value. If the LinkedList is empty, it throws an InvalidOperationException. How It Works When you call RemoveFirst(), the LinkedList performs the following operations − RemoveFirst() Operation ...
Read MoreHow to create user defined exceptions in C#?
In C#, you can create user-defined exceptions by deriving from the Exception class or one of its derived classes. User-defined exceptions allow you to create specific error types that are meaningful to your application domain, making error handling more precise and informative. Syntax Following is the basic syntax for creating a user-defined exception class − public class CustomExceptionName : Exception { public CustomExceptionName() : base() { } public CustomExceptionName(string message) : base(message) { } public CustomExceptionName(string message, Exception innerException) : ...
Read MoreIterators in C#
An iterator in C# performs custom iteration over a collection using the yield keyword. It returns elements one at a time and remembers its current position, making it memory-efficient for large datasets. Iterators implement lazy evaluation, meaning values are generated only when requested. Syntax Following is the syntax for creating an iterator method − public static IEnumerable MethodName() { // logic here yield return value; // more logic yield return anotherValue; } The yield break statement can be ...
Read MorePrivate Constructors and Singleton Classes in C#
A private constructor is a constructor that cannot be called from outside the class. It is primarily used in classes containing only static members or to implement the Singleton design pattern, which ensures that only one instance of a class can exist throughout the application's lifetime. Syntax Following is the syntax for declaring a private constructor − class ClassName { private ClassName() { // private constructor body } } Private Constructor for Static-Only Classes When a class contains only static members, ...
Read MoreWhat is unmanaged code in C#?
Unmanaged code in C# refers to code that executes outside the control of the .NET Common Language Runtime (CLR). This type of code is also known as unsafe code because it bypasses many of the safety mechanisms that make C# a managed language. In C#, unmanaged code typically involves the use of pointers and direct memory manipulation, similar to languages like C and C++. To use unmanaged code, you must explicitly mark code blocks with the unsafe keyword. Key Characteristics of Unmanaged Code Applications that are not under the control of the CLR are unmanaged. ...
Read MoreC# 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 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 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 MoreHow to declare and initialize a list in C#?
A List in C# is a generic collection that stores elements in a resizable array. You can declare and initialize a List in several ways, depending on whether you want to add elements immediately or start with an empty collection. Syntax Following is the basic syntax for declaring a List − List listName = new List(); Following is the syntax for initializing a List with values − List listName = new List() { value1, value2, value3 }; Empty List Declaration ...
Read MoreLinkedList in C#
The LinkedList class in C# is a doubly-linked list implementation found in the System.Collections.Generic namespace. Unlike arrays or lists, a linked list allows for efficient insertion and removal of elements at any position without the need to shift other elements. Each element in a LinkedList is stored in a node that contains the data and references to both the next and previous nodes. This structure enables fast insertion and deletion operations at the cost of direct indexed access. Syntax Following is the syntax for creating a LinkedList − LinkedList linkedList = new LinkedList(); ...
Read More