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
Csharp Articles
Page 130 of 196
String Formatting in C# to add padding on the right
String formatting in C# provides several ways to add padding to strings. Right padding aligns text to the left by adding spaces to the right side of the string until it reaches the specified width. Syntax Following is the syntax for right padding using composite formatting − string.Format("{0, -width}", value) Following is the syntax for right padding using string interpolation − $"{value, -width}" Following is the syntax for right padding using the PadRight() method − string.PadRight(totalWidth) string.PadRight(totalWidth, paddingChar) Right Padding Visualization ...
Read MoreString Formatting with ToString in C#
The ToString() method in C# is a powerful way to format values into strings using various format specifiers. It provides control over how numbers, dates, and other data types are displayed as text. Syntax Following is the basic syntax for using ToString() with format specifiers − variable.ToString("formatSpecifier"); For numeric formatting with custom patterns − number.ToString("000"); // Zero padding number.ToString("C"); // Currency format number.ToString("F2"); // Fixed-point with 2 decimals Using Zero Padding Format The zero padding format ensures ...
Read MoreSwapping Characters of a String in C#
Swapping characters in a string involves replacing specific characters with their counterparts while preserving the original structure. In C#, this can be accomplished using various approaches including the Select method with LINQ, character arrays, or the Replace method. Syntax Following is the syntax for swapping characters using LINQ's Select method − string.Select(character => condition ? replacement : character).ToArray() Following is the syntax for swapping using character arrays − char[] charArray = string.ToCharArray(); // modify charArray elements new string(charArray) Using LINQ Select Method The Select method processes each character individually ...
Read MoreHow to set a value to the element at the specified position in the one-dimensional array in C#
In C#, you can set a value to an element at a specific position in a one-dimensional array using the array indexing operator []. Array indexing in C# is zero-based, meaning the first element is at index 0, the second at index 1, and so on. Syntax Following is the syntax for setting a value to an array element at a specified position − arrayName[index] = value; Where index is the position (starting from 0) and value is the new value to be assigned. Using Array Index Assignment First, declare and initialize ...
Read MoreExit Methods in C# Application
In C# applications, there are several methods to exit or terminate the application. Each method serves different purposes and has specific use cases. Understanding when and how to use each exit method is crucial for proper application lifecycle management. Environment.Exit() Method The Environment.Exit() method terminates the entire process and returns an exit code to the operating system. This is the most common way to exit a console application. Syntax Environment.Exit(exitCode); The exitCode parameter indicates the success or failure status − 0 − Indicates successful completion Non-zero values − ...
Read MoreMatching strings with a wildcard in C#
Wildcard characters in C# allow you to match patterns in strings where some characters are unknown. The most common wildcard is the asterisk (*), which represents zero or more characters. C# provides several ways to implement wildcard matching, including regular expressions and custom methods. Using Regular Expressions for Wildcard Matching Regular expressions are the most powerful way to implement wildcard pattern matching. The pattern \bt\S*s\b matches words that start with 't' and end with 's' − Example using System; using System.Text.RegularExpressions; namespace Demo { public class Program { ...
Read MoreManaged code vs Unmanaged code in C#
In C#, code can be categorized as either managed or unmanaged based on how it is executed and controlled by the .NET runtime environment. Understanding the difference is crucial for C# developers working with system-level programming or interoperability scenarios. Managed Code Managed code is code whose execution is managed by the Common Language Runtime (CLR). The CLR provides automatic memory management, type safety, exception handling, and garbage collection. When you write C# code, it is compiled into Intermediate Language (IL) code, which is then executed by the CLR. Managed Code Execution Flow ...
Read MoreFile Searching using C#
File searching in C# allows you to programmatically locate and examine files within directories using the System.IO namespace. The DirectoryInfo and FileInfo classes provide powerful methods to search, filter, and retrieve detailed information about files and directories. Syntax Following is the syntax for creating a DirectoryInfo object and searching files − DirectoryInfo directory = new DirectoryInfo(@"path\to\directory"); FileInfo[] files = directory.GetFiles(); Following is the syntax for searching files with specific patterns − FileInfo[] files = directory.GetFiles("*.txt"); // Text files only FileInfo[] files = directory.GetFiles("*", SearchOption.AllDirectories); // ...
Read MoreHow to compile unsafe code in C#?
Unsafe code in C# allows direct memory manipulation using pointers, which bypasses the .NET garbage collector's safety mechanisms. To compile unsafe code, you need to enable unsafe context compilation through specific compiler settings. Command-Line Compilation For compiling unsafe code using the command-line compiler, you must specify the /unsafe switch − csc /unsafe filename.cs For example, to compile a program named one.cs containing unsafe code − csc /unsafe one.cs Visual Studio IDE Configuration In Visual Studio, you need to enable unsafe code compilation in the project properties. Follow these steps ...
Read MoreWhat is the difference between VAR and DYNAMIC keywords in C#?
The var and dynamic keywords in C# both allow you to declare variables without explicitly specifying their type, but they work very differently. The key difference is when type checking occurs — var is resolved at compile-time, while dynamic is resolved at runtime. Syntax Following is the syntax for declaring a var variable − var variableName = value; Following is the syntax for declaring a dynamic variable − dynamic variableName = value; Using var Keyword The var keyword creates statically typed variables. The compiler determines the type based on ...
Read More