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 864 of 2547
How to check if a C# list is empty?
In C#, there are several ways to check if a List is empty. The most common approaches include using the Count property, the Any() LINQ method, or comparing against an empty collection. Syntax Following are the common syntaxes for checking if a list is empty − // Using Count property if (list.Count == 0) { } // Using Any() method if (!list.Any()) { } // Using Count with comparison if (list.Count > 0) { } Using Count Property The Count property returns the number of elements in the list. When Count ...
Read MoreHow to access elements from an array in C#?
Arrays in C# are collections of elements that can be accessed using an index. Array indexing starts from 0, meaning the first element is at index 0, the second at index 1, and so on. Syntax Following is the syntax for accessing array elements by index − arrayName[index] Following is the syntax for declaring and initializing an array − dataType[] arrayName = new dataType[size] {element1, element2, element3}; Using Index to Access Elements Each element in an array has a specific position called an index. You can access any element ...
Read MoreGlobal and Local Variables in C#
In C#, variables are classified by their scope − the region of code where they can be accessed. The two main types are local variables (declared within methods) and global variables (which C# handles through static class members and namespace aliases). Local Variables A local variable is declared within a method, constructor, or block of code. Its scope is limited to that specific block, meaning it can only be accessed within the method or block where it was declared. Syntax dataType variableName; // or dataType variableName = value; Example using System; ...
Read MorePath methods in C#
To handle File Paths in C#, use the Path methods. These methods come under the System.IO namespace and provide a reliable way to work with file and directory paths across different operating systems. The Path class contains static methods that perform common operations on strings that contain file or directory path information. These methods handle path separators, invalid characters, and other platform-specific details automatically. Syntax Following is the syntax for commonly used Path methods − string extension = Path.GetExtension(path); string fileName = Path.GetFileName(path); string fileNameWithoutExt = Path.GetFileNameWithoutExtension(path); string directoryName = Path.GetDirectoryName(path); string fullPath = Path.GetFullPath(path); ...
Read MoreConstructors in C#
A constructor in C# is a special method that gets invoked automatically when an object is created. The constructor has the same name as the class and is used to initialize the object's state. Constructors do not have a return type, not even void. Syntax Following is the basic syntax for declaring a constructor − public class ClassName { public ClassName() { // constructor body } } Types of Constructors C# supports several types of constructors − Default ...
Read MoreC# equivalent to Java's Thread.setDaemon?
C# equivalent to Java's Thread.setDaemon is the concept of foreground and background threads. In C#, background threads serve the same purpose as daemon threads in Java − they run in the background and are automatically terminated when all foreground threads complete. When all foreground threads terminate, the CLR (Common Language Runtime) automatically shuts down the application, causing all background threads to be terminated immediately. Foreground threads keep the application alive until they complete execution. Syntax Following is the syntax to create a background thread using the IsBackground property − Thread thread = new Thread(methodName); thread.IsBackground ...
Read MoreIS vs AS Operators in C#
The is and as operators in C# are used for type checking and type conversion. The is operator checks if an object is compatible with a given type and returns a boolean value, while the as operator attempts to convert an object to a specified type and returns null if the conversion fails. Syntax Following is the syntax for the is operator − expr is type Following is the syntax for the as operator − expr as type Where expr is the expression to test and type is the target ...
Read MoreLambda Expressions in C#
A lambda expression in C# is an anonymous function that provides a concise way to write inline functions. Lambda expressions use the => operator, read as "goes to", which separates the input parameters from the expression body. Lambda expressions are commonly used with LINQ methods, event handlers, and delegates to create short, readable code without defining separate methods. Syntax Following is the basic syntax for lambda expressions − (input-parameters) => expression For single parameter, parentheses are optional − x => x * 2 For multiple statements, use curly braces ...
Read MoreC# Equivalent to Java Functional Interfaces
The C# equivalent to Java's functional interfaces is delegates. Delegates in C# provide the same functionality as functional interfaces in Java, allowing you to treat methods as first-class objects and use lambda expressions for concise code. Java functional interfaces define a contract with a single abstract method, while C# delegates directly represent method signatures that can be assigned lambda expressions or method references. Syntax Following is the syntax for declaring a delegate in C# − public delegate ReturnType DelegateName(ParameterType parameter); Following is the syntax for assigning a lambda expression to a delegate − ...
Read MoreC# Program to remove whitespaces in a string
In C#, there are several ways to remove whitespaces from a string. You can remove all spaces, only leading and trailing spaces, or all types of whitespace characters including tabs and newlines. Using String.Replace() Method The Replace()
Read More