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 Chandu yadav
Page 11 of 81
What does the @ prefix do on string literals in C#?
The @ prefix in C# creates a verbatim string literal, which means you don't need to escape special characters like backslashes, quotes, or newlines. This makes the string easier to read and write, especially for file paths, regular expressions, and multi-line text. Syntax Following is the syntax for verbatim string literals − @"string content here" To include a double quote inside a verbatim string, use two consecutive quotes − @"He said ""Hello"" to me" Using @ for File Paths The @ prefix eliminates the need to escape backslashes in ...
Read MoreWhat is the difference between initialization and assignment of values in C#?
In C#, initialization and assignment are two distinct concepts that are often confused. Understanding the difference is crucial for effective programming and memory management. Declaration vs Initialization vs Assignment Declaration creates a variable name, initialization allocates memory and sets initial values, and assignment changes the value of an already existing variable. Declaration → Initialization → Assignment Declaration int[] n; Creates variable name Initialization n = new int[5]; Allocates memory Assignment ...
Read MoreRepresent Int64 as a Hexadecimal String in C#
To represent Int64 as a hexadecimal string in C#, use the Convert.ToString() method and set the base as 16 for hexadecimal conversion. Int64 represents a 64-bit signed integer that can hold values from -9, 223, 372, 036, 854, 775, 808 to 9, 223, 372, 036, 854, 775, 807. Syntax Following is the syntax for converting Int64 to hexadecimal string − Convert.ToString(longValue, 16) You can also use the ToString() method with format specifiers − longValue.ToString("X") // Uppercase hex longValue.ToString("x") // Lowercase hex Using Convert.ToString() Method The ...
Read MoreRetrieve data value as a pointer in C#
A pointer is a variable whose value is the address of another variable. In C#, pointers can only be used in unsafe contexts. To retrieve the data stored at the location referenced by the pointer variable, you can use the dereference operator * or call the ToString() method on the pointer. Syntax Following is the syntax for declaring and using pointers in C# − unsafe { int* ptr = &variable; // Declare pointer and get address int value = *ptr; ...
Read MoreHow to insert an item in a list at a given position in C#?
To insert an item in an already created List, use the Insert() method. This method allows you to insert an element at a specific index position within the list, shifting all subsequent elements to the right. Syntax Following is the syntax for the Insert() method − list.Insert(index, item); Parameters index − The zero-based index at which the item should be inserted. item − The object to insert into the list. Insert Operation at Index 3 Before Insert: ...
Read MoreC# Linq Except Method
The Except() method in LINQ is used to find the set difference between two collections. It returns elements from the first collection that are not present in the second collection, effectively performing a set subtraction operation. Syntax Following is the syntax for the Except() method − public static IEnumerable Except( this IEnumerable first, IEnumerable second ) With custom equality comparer − public static IEnumerable Except( this IEnumerable first, IEnumerable second, IEqualityComparer comparer ...
Read MoreC# program to get the file name in C#
In C#, you can extract the file name from a full path using the Path.GetFileName() method from the System.IO namespace. This method returns only the file name and extension, without the directory path. Syntax Following is the syntax for using Path.GetFileName() method − string fileName = Path.GetFileName(filePath); Parameters filePath − A string containing the full path to the file. Return Value The method returns a string containing the file name and extension. If the path ends with a directory separator, it returns an empty string. Using ...
Read MoreWhat are user defined data types in C#?
User-defined data types in C# allow developers to create custom types beyond the built-in primitive types. The main user-defined data types in C# are structures, enumerations, classes, interfaces, and delegates. This article focuses on structures and enumerations as fundamental user-defined types. Structure In C#, a structure is a value type data type that helps you group related data of various types into a single unit. The struct keyword is used for creating a structure. Syntax public struct StructName { // fields, properties, methods, constructors public dataType field1; ...
Read MoreHow to iterate over a C# tuple?
A tuple in C# is a data structure that holds multiple values of different types. Since tuples have a fixed structure, you cannot iterate over them using traditional loops like arrays or collections. Instead, you access tuple elements individually using their Item properties or through deconstruction. Syntax To create a tuple and access its elements − // Creating a tuple Tuple tuple = new Tuple(100, "Tom"); // Accessing individual elements tuple.Item1 // first element tuple.Item2 // second element For modern C# (7.0+), you can use tuple literals − var ...
Read MoreConvert.ChangeType Method in C#
The Convert.ChangeType() method in C# converts a value to a specified type. It returns an object of the target type whose value is equivalent to the original object. This method is particularly useful when you need to perform type conversions at runtime or when working with generic code. Syntax Following is the syntax for the Convert.ChangeType() method − public static object ChangeType(object value, Type conversionType) public static object ChangeType(object value, TypeCode typeCode) Parameters value − The object to convert. conversionType − The target Type to convert to. typeCode − The TypeCode representing ...
Read More