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
Server Side Programming Articles
Page 783 of 2109
Generics vs non-generics in C#
There are two main types of collections in C#: generic collections and non-generic collections. The primary difference lies in type safety, performance, and how they handle different data types. Generic Collections Generic collections are type-safe and hold elements of the same data type. They provide better performance as they avoid boxing and unboxing operations for value types. Key Features Type Safety: Compile-time type checking prevents runtime errors Better Performance: No boxing/unboxing for value types IntelliSense Support: Better code completion and error detection Common Generic Collections List − Dynamic array of type ...
Read MoreWhy do we use internal keyword in C#?
The internal keyword in C# is an access modifier that provides assembly-level access. It allows class members to be accessible within the same assembly but restricts access from other assemblies, making it ideal for creating APIs that should only be used internally within your project. Syntax Following is the syntax for declaring internal members − internal class ClassName { // class accessible within assembly only } class MyClass { internal int field; // field accessible within assembly ...
Read MoreHow to compare two tuples in C#?
Tuple comparison was introduced in C# 7.3, allowing you to easily compare two tuples using equality operators. Tuples are compared element-wise, meaning each corresponding element in both tuples must be equal for the tuples to be considered equal. Syntax Following is the syntax for comparing tuples using the equality operator − bool result = tuple1 == tuple2; You can also use the inequality operator − bool result = tuple1 != tuple2; How Tuple Comparison Works Tuple comparison in C# follows these rules: Tuples must have the same ...
Read MoreWhy does the indexing start with zero in C# arrays?
In C#, array indexing starts at zero because arrays are fundamentally based on memory addressing and offset calculations. This design choice has deep roots in computer science and provides both efficiency and logical consistency. When an array is created, it occupies a contiguous block of memory. The array name acts as a pointer to the first element's memory address, and accessing elements involves calculating offsets from this base address. Memory Layout and Addressing Consider an integer array with 5 elements stored in memory. Since each int occupies 4 bytes, the memory layout would look like this − ...
Read MoreHow are values assigned to arrays in C#?
Declaring an array does not initialize the array in memory. When the array variable is initialized, you can assign values to the array using several different approaches in C#. Array is a reference type, so you need to use the new keyword to create an instance of the array. There are multiple ways to assign values to arrays, each with its own syntax and use cases. Syntax Following is the syntax for declaring and initializing arrays − // Declaration with size datatype[] arrayName = new datatype[size]; // Assignment using index arrayName[index] = value; ...
Read MoreHow 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 MoreWhat is the difference between a list and an array in C#?
An array stores a fixed-size sequential collection of elements of the same type, whereas a list is a generic collection that can dynamically resize during runtime. Understanding the differences between arrays and lists is crucial for choosing the right data structure for your C# applications. Syntax Following is the syntax for declaring and initializing an array − dataType[] arrayName = new dataType[size]; dataType[] arrayName = {value1, value2, value3}; Following is the syntax for declaring and initializing a list − List listName = new List(); List listName = new List {value1, value2, value3}; ...
Read Morefinal, finally and finalize in C#
In C#, the terms final, finally, and finalize serve different purposes. While final doesn't exist in C#, finally and finalize are crucial concepts for exception handling and resource cleanup respectively. final (sealed in C#) C# does not have a final keyword like Java. Instead, C# uses the sealed keyword to achieve similar functionality − public sealed class SealedClass { // cannot be inherited } public override sealed void SealedMethod() { // cannot be overridden further } The sealed keyword prevents overriding of methods or inheritance of classes. ...
Read MoreStreams and Byte Streams in C#
A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream. Streams in C# provide a unified interface for reading and writing data, whether it's from files, memory, or network connections. The type of streams includes − Byte Streams − They handle raw binary data and include Stream, FileStream, MemoryStream, and BufferedStream. Character Streams − They handle text data and include TextReader, TextWriter, StreamReader, StreamWriter, and other text-based streams. Byte ...
Read MoreString format for DateTime in C#
The String.Format method in C# provides powerful formatting capabilities for DateTime objects. You can use various format specifiers to display dates and times in different formats, from simple year displays to complex custom patterns. Syntax Following is the syntax for formatting DateTime using String.Format − String.Format("{0:format_specifier}", dateTimeObject) Common format specifiers include − y − Year (1 digit minimum) yy − Year (2 digits) yyyy − Year (4 digits) M − Month (1-12) MM − Month (01-12) MMM − Month abbreviation (Jan, Feb, etc.) MMMM − Full month name (January, February, etc.) d ...
Read More