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 Samual Sam
Page 20 of 151
C# Linq SkipLast Method
The SkipLast() method in C# LINQ is used to skip a specified number of elements from the end of a sequence and return the remaining elements. This method is particularly useful when you need to exclude the last few items from a collection. Syntax Following is the syntax for the SkipLast() method − public static IEnumerable SkipLast( this IEnumerable source, int count ) Parameters source − The sequence to skip elements from. count − The number of elements to skip from the end ...
Read MoreHow to split a string with a string delimiter in C#?
String splitting in C# is a common operation used to divide a string into substrings based on specified delimiters. The Split() method provides multiple overloads to handle different types of delimiters including characters, strings, and arrays. Syntax Following are the common syntax forms for splitting strings − // Split by single character string[] result = str.Split(', '); // Split by character array char[] delimiters = {', ', ';', '|'}; string[] result = str.Split(delimiters); // Split by string delimiter string[] result = str.Split(new string[] {"||"}, StringSplitOptions.None); Using Character Delimiters The simplest way ...
Read MoreWhat is a dictionary in C#?
A Dictionary in C# is a generic collection that stores data in key-value pairs. It belongs to the System.Collections.Generic namespace and provides fast lookups based on unique keys. Each key in a Dictionary must be unique, while values can be duplicated. The Dictionary class implements the IDictionary interface and uses hash tables internally for efficient data retrieval. Syntax Following is the syntax for declaring a Dictionary − Dictionary dictionaryName = new Dictionary(); You can also use the interface type for declaration − IDictionary dictionaryName = new Dictionary(); Creating and ...
Read MoreC# Program to write an array to a file
Writing an array to a file in C# can be accomplished using the File.WriteAllLines method from the System.IO namespace. This method writes each array element as a separate line to the specified file. Syntax Following is the syntax for using File.WriteAllLines − File.WriteAllLines(string path, string[] contents); Parameters path − The file path where the array will be written contents − The string array to write to the file Using WriteAllLines Method The WriteAllLines method automatically creates the file if it doesn't exist and overwrites it ...
Read MoreC# program to convert a list of characters into a string
Converting a list of characters into a string is a common operation in C#. There are several approaches to accomplish this task, each suitable for different scenarios and data structures. Syntax Following are the main syntax patterns for converting characters to strings − // Using string constructor with char array string result = new string(charArray); // Using string.Join() method string result = string.Join("", charList); // Using StringBuilder class StringBuilder sb = new StringBuilder(); sb.Append(charArray); string result = sb.ToString(); Using String Constructor with Character Array The most direct approach is using the ...
Read MoreWhy we do not have global variables in C#?
C# does not have global variables like those found in C or C++. Instead, C# follows an object-oriented paradigm where all data and methods must be contained within classes or structures. The global namespace alias (global::) is used to resolve naming conflicts between namespaces, not to access global variables. Why C# Doesn't Have Global Variables C# was designed with several principles that eliminate the need for global variables − Type Safety: Global variables can lead to unpredictable behavior and make debugging difficult. Object-Oriented Design: Everything must belong to a class or struct, promoting ...
Read MoreC# Program to display the name of the directory
In C# programming, you can display the name of a directory using the DirectoryInfo class from the System.IO namespace. This class provides properties and methods to work with directory information, including retrieving just the directory name without the full path. Syntax Following is the syntax for creating a DirectoryInfo object and accessing the directory name − DirectoryInfo dir = new DirectoryInfo(directoryPath); string directoryName = dir.Name; Parameters directoryPath − A string representing the path to the directory Return Value The Name property returns a string containing only the name of ...
Read MoreC# program to count occurrences of a word in string
Counting occurrences of a specific word in a string is a common string manipulation task in C#. This can be achieved using various approaches like the IndexOf() method, regular expressions, or LINQ methods. Using IndexOf() Method The IndexOf() method searches for the first occurrence of a substring and returns its position. By combining it with a loop, we can count all occurrences − using System; class Program { static void Main() { string str = "Hello World! Hello Universe! Hello!"; ...
Read MoreWhat is the best way to iterate over a Dictionary in C#?
A Dictionary is a collection of key-value pairs in C#. The Dictionary class is included in the System.Collections.Generic namespace and provides several efficient ways to iterate through its elements. There are multiple approaches to iterate over a Dictionary, each with its own advantages depending on whether you need keys, values, or both. Syntax Following are the common syntaxes for iterating over a Dictionary − // Using foreach with KeyValuePair foreach (KeyValuePair item in dictionary) { // Access item.Key and item.Value } // Using foreach with var foreach (var item in dictionary) ...
Read MoreWhat is static binding in C#?
Static binding in C# refers to the process of linking a function call to its implementation at compile time. This is also known as early binding or compile-time polymorphism. The compiler determines which method to call based on the method signature and the types of arguments passed. C# provides two main techniques to implement static binding through static polymorphism − Function Overloading − Multiple methods with the same name but different parameters Operator Overloading − Custom implementations for operators like +, -, *, etc. Static Binding Process ...
Read More