Csharp Articles

Page 130 of 196

String Formatting in C# to add padding on the right

vanithasree
vanithasree
Updated on 17-Mar-2026 334 Views

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 More

String Formatting with ToString in C#

varun
varun
Updated on 17-Mar-2026 793 Views

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 More

Swapping Characters of a String in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 2K+ Views

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 More

How to set a value to the element at the specified position in the one-dimensional array in C#

radhakrishna
radhakrishna
Updated on 17-Mar-2026 407 Views

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 More

Exit Methods in C# Application

radhakrishna
radhakrishna
Updated on 17-Mar-2026 12K+ Views

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 More

Matching strings with a wildcard in C#

varma
varma
Updated on 17-Mar-2026 5K+ Views

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 More

Managed code vs Unmanaged code in C#

Prabhas
Prabhas
Updated on 17-Mar-2026 4K+ Views

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 More

File Searching using C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 636 Views

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 More

How to compile unsafe code in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 3K+ Views

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 More

What is the difference between VAR and DYNAMIC keywords in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 711 Views

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
Showing 1291–1300 of 1,951 articles
« Prev 1 128 129 130 131 132 196 Next »
Advertisements