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 karthikeya Boyini
Page 7 of 143
How to use Remove, RemoveAt, RemoveRange methods in C# list collections?
C# provides several methods to remove elements from List collections. The Remove() method removes the first occurrence of a specific value, RemoveAt() removes an element at a specific index, and RemoveRange() removes multiple consecutive elements. Syntax Following is the syntax for the three removal methods − // Remove by value (first occurrence) bool Remove(T item) // Remove by index void RemoveAt(int index) // Remove range of elements void RemoveRange(int index, int count) Parameters Remove(T item): The item to remove from the list. Returns true if item is found and removed. ...
Read MoreConvert.ToInt32 Method in C#
The Convert.ToInt32() method in C# converts a specified value to a 32-bit signed integer. This method provides type conversion from various data types including string, double, float, decimal, and bool to int. The method uses rounding to nearest even (banker's rounding) when converting floating-point numbers, and throws exceptions for invalid conversions like null strings or out-of-range values. Syntax Following are the common overloads of Convert.ToInt32() method − Convert.ToInt32(object value) Convert.ToInt32(string value) Convert.ToInt32(double value) Convert.ToInt32(bool value) Parameters value − The value to convert to a 32-bit signed integer. Return Value ...
Read MoreWhat is the Item property of SortedList class in C#?
The Item property of the SortedList class in C# gets and sets the value associated with a specific key in the SortedList. It allows you to access and modify elements using the indexer syntax [key], making SortedList operations intuitive and similar to working with arrays or dictionaries. The Item property can also be used to add new elements directly. If the key does not exist, it creates a new key-value pair. If the key already exists, it overwrites the existing value with the new one. Syntax Following is the syntax for using the Item property − ...
Read MoreHow to use the sleep method in C#?
The Thread.Sleep() method in C# is used to pause the execution of the current thread for a specified period of time. This is commonly used to introduce delays in program execution or to simulate time-consuming operations. Syntax The Thread.Sleep() method has two overloads − Thread.Sleep(int millisecondsTimeout); Thread.Sleep(TimeSpan timeout); Parameters millisecondsTimeout − The number of milliseconds for which the thread is suspended. Use Timeout.Infinite (-1) to suspend the thread indefinitely. timeout − A TimeSpan that sets the amount of time for which the thread is suspended. Using Thread.Sleep() with Milliseconds ...
Read MoreHow to check if a string is a valid keyword in C#?
To check if a string is a valid keyword in C#, you can use the IsValidIdentifier method from the CodeDomProvider class. This method determines whether a given string is a valid identifier or a reserved keyword in C#. The IsValidIdentifier method returns true if the string is a valid identifier (like variable names, method names), and false if it's a reserved keyword (like for, if, class, etc.). Syntax Following is the syntax for using IsValidIdentifier method − CodeDomProvider provider = CodeDomProvider.CreateProvider("C#"); bool isIdentifier = provider.IsValidIdentifier(stringToCheck); Using IsValidIdentifier to Check Keywords Here's how ...
Read MoreWhat is the IsReadOnly property of ArrayList class in C#?
The IsReadOnly property of the ArrayList class in C# returns a boolean value indicating whether the ArrayList is read-only. A read-only ArrayList cannot be modified after creation − you cannot add, remove, or change elements. Syntax Following is the syntax for using the IsReadOnly property − bool isReadOnly = arrayList.IsReadOnly; Return Value The IsReadOnly property returns − true − if the ArrayList is read-only false − if the ArrayList can be modified Using IsReadOnly with Regular ArrayList A regular ArrayList created using the default constructor is always modifiable, ...
Read MoreHow do you loop through a C# array?
To loop through an array in C#, you can use several loop types including for, foreach, while, and do...while loops. Each loop provides different ways to iterate through array elements and access their values. The most commonly used loops for arrays are the for loop (when you need index access) and the foreach loop (when you only need element values). Syntax Following is the syntax for different loop types with arrays − // for loop for (int i = 0; i < array.Length; i++) { // access array[i] } // ...
Read MoreSingleton Class in C#
A Singleton class in C# is a design pattern that ensures only one instance of a class can be created throughout the application's lifetime. It provides a global point of access to that instance and is commonly used for logging, database connections, and configuration management. The key principle of the Singleton pattern is to restrict instantiation of a class to a single object by using a private constructor and providing a static method or property to access the instance. Syntax Following is the basic syntax for implementing a Singleton class − public class Singleton { ...
Read MoreHow to Initialize and Compare Strings in C#?
String initialization and comparison are fundamental operations in C# programming. C# provides multiple ways to initialize strings and several methods to compare them effectively. String Initialization There are several ways to initialize a string in C# − // Direct assignment string str1 = "Hello, World!"; // Using string constructor string str2 = new string("Welcome"); // Empty string initialization string str3 = ""; string str4 = string.Empty; Example using System; class Program { static void Main(string[] args) { ...
Read MoreConvert.ToSByte Method in C#
The Convert.ToSByte method in C# converts a specified value to an 8-bit signed integer (sbyte). The sbyte data type can store values from -128 to 127 and is useful when you need to work with small signed integers while conserving memory. Syntax The Convert.ToSByte method has several overloads to handle different data types − Convert.ToSByte(value) Convert.ToSByte(value, IFormatProvider) Convert.ToSByte(value, fromBase) Parameters value − The value to be converted to sbyte. Can be of various types including bool, char, decimal, double, float, int, string, etc. provider − An object that supplies culture-specific formatting information ...
Read More