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 3 of 143
Multiple Where clause in C# Linq
In C# LINQ, you can apply multiple where clauses to filter collections based on different conditions. Each additional where clause further narrows down the results, creating a logical AND relationship between the conditions. Syntax There are two main approaches to using multiple where clauses − Query Syntax with Multiple Where Clauses: var result = from item in collection where condition1 where condition2 ...
Read MoreC# program to generate secure random numbers
For secure random numbers, use the RNGCryptoServiceProvider class or the newer RandomNumberGenerator class. These implement cryptographic random number generators that are suitable for security-sensitive applications like generating passwords, tokens, or encryption keys. Secure random numbers differ from regular Random class numbers because they use entropy from the operating system and are cryptographically secure, making them unpredictable even if an attacker knows some previously generated values. Syntax Following is the syntax for generating secure random bytes − using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider()) { byte[] randomBytes = new byte[4]; crypto.GetBytes(randomBytes); ...
Read MoreHow to calculate the Size of Folder using C#?
To calculate the size of a folder in C#, you need to traverse through all files in the folder and its subdirectories, then sum up their sizes. The DirectoryInfo class provides methods to enumerate files and directories, making this task straightforward. Syntax Following is the syntax for getting folder information and enumerating files − DirectoryInfo info = new DirectoryInfo(@"C:\FolderPath"); long size = info.EnumerateFiles("*", SearchOption.AllDirectories).Sum(file => file.Length); Using DirectoryInfo for Simple Folder Size The most basic approach uses DirectoryInfo.EnumerateFiles() with SearchOption.AllDirectories to include all subdirectories − using System; using System.IO; using System.Linq; ...
Read MoreC# Linq Where Method
The Where method in LINQ is used to filter elements from a collection based on a specified condition (predicate). It returns a new IEnumerable containing only the elements that satisfy the given criteria. Syntax The Where method has two overloads − // Filter based on element value only public static IEnumerable Where(this IEnumerable source, Func predicate) // Filter based on element value and index public static IEnumerable Where(this IEnumerable source, Func predicate) Parameters source − The input sequence to filter predicate − A function that tests each element for a condition ...
Read MoreC# Nullable Datetime
The nullable DateTime in C# allows you to assign null values to DateTime variables, which is useful when a date might not be available or applicable. This is particularly valuable in database operations where date fields can be NULL, or when dealing with optional date parameters. Syntax A nullable DateTime is declared using the question mark (?) syntax − DateTime? variableName = null; Alternatively, you can use the full Nullable syntax − Nullable variableName = null; Key Properties and Methods Nullable DateTime provides several useful properties and methods − ...
Read MoreC# Program to read all the lines of a file at once
The File.ReadAllText() method in C# reads all the text from a file and returns it as a single string. This method is part of the System.IO namespace and provides a simple way to read entire file contents at once. When you need to read all lines from a text file in one operation, ReadAllText() is more efficient than reading line by line, especially for smaller files. Syntax Following is the syntax for the File.ReadAllText() method − public static string ReadAllText(string path); Parameters path − The file path to read from. Can ...
Read MoreCharacter constants vs String literals in C#
In C#, both character constants and string literals are used to represent text data, but they serve different purposes and have distinct syntax rules. Character constants represent single characters, while string literals represent sequences of characters (text). Character Constants Character constants are enclosed in single quotes and represent a single character. They are stored in variables of type char. Syntax char variableName = 'character'; Character constants can be − Plain characters − 'x', 'A', '5' Escape sequences − '', '\t', '' Unicode characters − '\u0041' (represents 'A') Example ...
Read MoreWhat is method overloading in C#?
Method overloading in C# allows you to define multiple methods with the same name but different parameters. This enables you to create methods that perform similar operations but accept different types or numbers of arguments. Method overloading can be achieved by changing the number of parameters, the data types of parameters, or the order of parameters. The compiler determines which method to call based on the arguments passed at runtime. Syntax Following is the syntax for method overloading − public returnType MethodName(type1 param1) { } public returnType MethodName(type1 param1, type2 param2) { } public returnType ...
Read MoreLong Time ("T") Format Specifier in C#
The Long Time ("T") format specifier in C# is a standard date and time format specifier that displays the time portion of a DateTime value in a long format. This format is culture-sensitive and displays the full time representation including hours, minutes, seconds, and AM/PM designator where applicable. The "T" format specifier is defined by the DateTimeFormatInfo.LongTimePattern property and typically follows the pattern HH:mm:ss for 24-hour format or includes AM/PM for 12-hour format, depending on the culture. Syntax Following is the syntax for using the Long Time format specifier − dateTime.ToString("T") dateTime.ToString("T", CultureInfo.CreateSpecificCulture("culture-name")) ...
Read MoreAn array of streams in C#
An array of streams in C# refers to using arrays or collections along with stream objects like StreamWriter and StreamReader to perform file I/O operations. This approach is commonly used when you need to write multiple data items from an array to a file or read file content into an array structure. Syntax Following is the syntax for writing array data to a file using StreamWriter − using (StreamWriter writer = new StreamWriter("filename.txt")) { foreach (dataType item in arrayName) { writer.WriteLine(item); ...
Read More