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 Arjun Thakur
749 articles
C# Nested Classes
A nested class is a class declared inside another enclosing class. It is a member of its enclosing class, and nested classes can access private members of their enclosing class. However, the enclosing class cannot directly access private members of the nested class without creating an instance. Syntax Following is the syntax for declaring a nested class − public class OuterClass { // outer class members public class NestedClass { // nested class members } } ...
Read MoreHow to add items to a list in C#?
In C#, you can add items to a List using several methods. The most common approach is using the Add() method to add single items, but there are also methods to add multiple items at once. Syntax Following is the syntax for creating a list and adding items − var listName = new List(); listName.Add(item); To add multiple items at once − listName.AddRange(collection); Using Add() Method The Add() method adds a single item to the end of the list − using System; using System.Collections.Generic; public class ...
Read MoreC# OfType() Method
The OfType() method in C# is a LINQ extension method that filters a collection based on the type of its elements. It returns only the elements that match the specified type, making it useful for working with heterogeneous collections containing different data types. Syntax Following is the syntax for the OfType() method − public static IEnumerable OfType(this IEnumerable source) The method can be used with both method syntax and query syntax − // Method syntax var result = collection.OfType(); // Query syntax var result = from item in collection.OfType() select item; ...
Read MoreCopyOnWriteArrayList version in C#
While Java has CopyOnWriteArrayList, C# does not have a direct equivalent. Instead, the SynchronizedCollection class in C# provides thread-safe collection operations. Unlike Java's copy-on-write approach, SynchronizedCollection uses locking mechanisms to ensure thread safety. Syntax Following is the syntax for declaring a SynchronizedCollection − public class SynchronizedCollection : IList, ICollection, IEnumerable, IEnumerable, IList, ICollection Where T is the type of objects in the collection. Properties The SynchronizedCollection class provides the following key properties − Property Description Count Gets the number of ...
Read MoreWhat are pointers in C#?
A pointer is a variable that stores the memory address of another variable. Unlike reference types, pointers provide direct access to memory locations, making them powerful but potentially unsafe. In C#, pointers can only be used within unsafe code blocks or methods. This restriction exists because pointers bypass .NET's garbage collection and type safety mechanisms. Syntax Following is the syntax for declaring a pointer − type *pointerName; Following is the syntax for getting the address of a variable − type *pointer = &variable; Following is the syntax for dereferencing ...
Read MoreWhat is the IsReadOnly property of SortedList class in C#?
The IsReadOnly property of the SortedList class in C# returns a boolean value indicating whether the collection is read-only. A read-only collection cannot be modified after creation − you cannot add, remove, or update elements. For standard SortedList instances created using the default constructor, this property always returns false, meaning the collection is modifiable. Syntax Following is the syntax for using the IsReadOnly property − bool isReadOnly = sortedList.IsReadOnly; Return Value The IsReadOnly property returns − true − if the SortedList is read-only false − if the SortedList can be ...
Read MoreHow to append a second list to an existing list in C#?
Use the AddRange() method to append a second list to an existing list in C#. The AddRange() method adds all elements from one collection to the end of another list, effectively combining two lists into one. Syntax Following is the syntax for using AddRange() method − list1.AddRange(list2); Parameters collection − The collection whose elements should be added to the end of the List. The collection itself cannot be null, but it can contain elements that are null. Using AddRange() Method The AddRange() method modifies the original list by adding ...
Read MoreHow to compile and execute C# programs on Linux?
To compile and execute C# programs on Linux, you have several options. The modern approach is to use the .NET SDK, which provides a cross-platform runtime and compiler. You can also use IDEs like MonoDevelop or Visual Studio Code for development. The .NET SDK is Microsoft's official cross-platform development platform that runs natively on Linux. MonoDevelop is an open-source IDE that allows you to run C# on multiple platforms including Windows, Linux, and macOS. MonoDevelop is also known as Xamarin Studio and includes a C# compiler. Installing .NET SDK on Linux The .NET SDK is the recommended ...
Read MoreWhat is composition in C#?
Composition in C# is a design principle where one class contains an instance of another class as a private member, creating a "has-a" relationship. In composition, the contained object (child) cannot exist independently of the container object (parent). When the parent object is destroyed, the child object is also destroyed, establishing a strong ownership relationship. Composition represents a part-of relationship, which is a special type of association that implies strong coupling between objects. Syntax Following is the basic syntax for implementing composition in C# − public class ContainedClass { // properties and ...
Read MoreWhat is difference between internal and private modifiers in C#?
The internal and private access modifiers in C# control the visibility and accessibility of class members. Understanding their differences is crucial for designing well-structured applications with proper encapsulation. The internal modifier allows access within the same assembly, while private restricts access to the same class only. Both serve different purposes in controlling member visibility. Syntax Following is the syntax for internal access modifier − internal dataType memberName; internal void MethodName() { } Following is the syntax for private access modifier − private dataType memberName; private void MethodName() { } ...
Read More