Csharp Articles

Page 66 of 196

What is an alternative to string.Replace that is case-insensitive in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 3K+ Views

The standard string.Replace() method in C# is case-sensitive by default. When you need case-insensitive string replacement, there are several alternative approaches you can use. The most common methods include using regular expressions, the StringComparison parameter (available in .NET 5+), or creating custom replacement methods. Using Regular Expressions for Case-Insensitive Replace Regular expressions provide the most flexible approach for case-insensitive string replacement using RegexOptions.IgnoreCase − using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { string str = "Cricket Team"; ...

Read More

How to parse a string into a nullable int in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 6K+ Views

Parsing a string into a nullable int in C# allows you to handle cases where the string might not represent a valid integer. A nullable int (int?) can store either an integer value or null, making it ideal for scenarios where conversion might fail. C# provides several approaches to parse strings into nullable integers, from extension methods to built-in parsing techniques that handle invalid input gracefully. Syntax Following is the syntax for declaring a nullable int − int? nullableInt = null; int? nullableInt = 42; Following is the syntax for parsing using int.TryParse() ...

Read More

What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 5K+ Views

Converting seconds to a formatted time string in the format Hour:Minutes:Seconds:Milliseconds is a common requirement in C# applications. The most efficient approach is using the TimeSpan structure, which provides built-in methods for time calculations and formatting. TimeSpan Structure TimeSpan represents a time interval and provides properties like Hours, Minutes, Seconds, and Milliseconds for easy access to time components. The TimeSpan.FromSeconds() method creates a TimeSpan object from a given number of seconds. Syntax Following is the syntax for converting seconds using TimeSpan.FromSeconds() − TimeSpan timeSpan = TimeSpan.FromSeconds(totalSeconds); string formatted = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", ...

Read More

How to determine if C# .NET Core is installed?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 3K+ Views

Determining if C# .NET Core is installed on your system is essential for development and deployment. The dotnet command-line interface (CLI) provides several built-in options to check installation status, versions, and available components. These commands will display environment information if .NET Core is installed, or throw an error if it's not found. Using dotnet --info The --info option prints detailed information about the .NET Core installation and machine environment, including the current operating system and commit SHA of the .NET Core version − dotnet --info This command provides comprehensive details about your .NET installation, ...

Read More

How to resize an Image C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 668 Views

Image resizing in C# is a common requirement for optimizing storage space, improving loading times, and adjusting images for different display contexts. The System.Drawing namespace provides the Bitmap class and related classes to handle image manipulation tasks including resizing, compression, and format conversion. A bitmap consists of pixel data for a graphics image and its attributes. GDI+ supports multiple file formats including BMP, GIF, EXIF, JPG, PNG, and TIFF. You can create images from files, streams, and other sources using Bitmap constructors and save them using the Save method. Basic Image Resizing The most straightforward approach to ...

Read More

How to convert XML to Json and Json back to XML using Newtonsoft.json?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 3K+ Views

Newtonsoft.Json (Json.NET) provides powerful methods for converting between XML and JSON formats. The library preserves all XML features including elements, attributes, text content, comments, character data, processing instructions, namespaces, and XML declarations during conversion. Required NuGet Package Install the Newtonsoft.Json package to use these conversion methods − Install-Package Newtonsoft.Json Key Methods The JsonConvert class provides two essential methods for XML-JSON conversion − SerializeXmlNode() − Converts XML to JSON format DeserializeXmlNode() − Converts JSON back to XML format Converting XML to JSON Use SerializeXmlNode() to convert an XmlDocument or ...

Read More

How to copy files into a directory in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 4K+ Views

To copy files in C#, the File.Copy method is the primary approach. This method allows you to copy individual files from one location to another, with options to control whether existing files should be overwritten. Syntax The File.Copy method has two overloads − File.Copy(string sourceFileName, string destFileName) File.Copy(string sourceFileName, string destFileName, bool overwrite) Parameters sourceFileName − The file to copy. destFileName − The name of the destination file. This cannot be a directory. overwrite − true if the destination file should be overwritten if it already exists; otherwise, false. ...

Read More

What is use of fluent Validation in C# and how to use in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

FluentValidation is a powerful .NET library for building strongly-typed validation rules using a fluent interface and lambda expressions. It helps separate validation logic from your domain models, making your code cleaner and more maintainable by centralizing validation rules in dedicated validator classes. The library provides an intuitive way to define complex validation rules with built-in validators, custom validation methods, and detailed error messaging. It's particularly useful in web applications, APIs, and any scenario where robust input validation is required. Installation To use FluentValidation in your project, install the NuGet package − Syntax ...

Read More

How to valid DateofBirth using fluent Validation in C# if it exceeds current year?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 5K+ Views

FluentValidation in C# provides a powerful way to validate model properties using a fluent interface. When validating date of birth, you need to ensure the date doesn't exceed the current year and falls within a reasonable age range. This validation is essential for preventing future dates in birth date fields and ensuring data integrity in applications dealing with user registration or profile management. Syntax Following is the syntax for creating a date of birth validation rule using FluentValidation − RuleFor(p => p.DateOfBirth) .Must(BeAValidAge) .WithMessage("Invalid {PropertyName}"); ...

Read More

How to check Minlength and Maxlength validation of a property in C# using Fluent Validation?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 3K+ Views

FluentValidation is a popular .NET validation library that provides an easy way to validate model properties. The MinLength and MaxLength validators ensure that string properties meet specific length requirements, making them essential for input validation scenarios. Syntax Following is the syntax for using MaximumLength validator − RuleFor(x => x.PropertyName).MaximumLength(maxValue); Following is the syntax for using MinimumLength validator − RuleFor(x => x.PropertyName).MinimumLength(minValue); MaxLength Validator The MaximumLength validator ensures that the length of a string property does not exceed the specified maximum value. This validator is only valid on string properties. ...

Read More
Showing 651–660 of 1,951 articles
« Prev 1 64 65 66 67 68 196 Next »
Advertisements