Programming Articles

Page 866 of 2547

Convert a ValueTuple to a Tuple in C#

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

In C#, you can easily convert a ValueTuple to a Tuple using the ToTuple() method. ValueTuples are value types introduced in C# 7.0, while Tuples are reference types that have been available since earlier versions of C#. Note − For .NET Framework projects, you may need to add the System.ValueTuple NuGet package to use ValueTuple features. Syntax Following is the syntax for converting a ValueTuple to a Tuple using the ToTuple() method − var valueTuple = (value1, value2, value3); Tuple tuple = valueTuple.ToTuple(); Converting ValueTuple to Tuple Example using System; ...

Read More

Difference between Abstract Class and Interface in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 797 Views

Both abstract classes and interfaces in C# define contracts for derived classes to implement, but they serve different purposes and have distinct characteristics. An interface defines a contract with method signatures that implementing classes must provide, while an abstract class can provide both abstract methods and concrete implementations that derived classes inherit. Understanding the key differences helps you choose the right approach for your object-oriented design needs. Syntax Following is the syntax for declaring an interface − public interface IInterfaceName { void Method1(); int Property1 { get; ...

Read More

DateTime.Add() Method in C#

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

The DateTime.Add() method in C# is used to return a new DateTime that adds the value of the specified TimeSpan to the value of this instance. This method allows you to add or subtract time intervals including days, hours, minutes, seconds, and milliseconds. Syntax Following is the syntax − public DateTime Add(TimeSpan value); Parameters value − A positive or negative TimeSpan that represents the time interval to add to the current DateTime instance. Return Value Returns a new DateTime object whose value is the sum of the ...

Read More

C# Round-trip ("R") Format Specifier

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

The round-trip ("R") format specifier in C# ensures that a numeric value converted to a string can be parsed back into the same exact numeric value without any precision loss. This format specifier is supported for Single, Double, and BigInteger types. The "R" specifier is particularly useful when you need to serialize floating-point numbers to strings and then deserialize them back while maintaining perfect accuracy. Syntax Following is the syntax for using the round-trip format specifier − value.ToString("R") value.ToString("R", CultureInfo.InvariantCulture) How It Works The round-trip format specifier automatically determines the minimum number ...

Read More

Difference between Boxing and Unboxing in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 1K+ Views

Boxing converts a value type to an object type, whereas unboxing converts an object type back to the value type. These operations are fundamental concepts in C# that involve moving data between the stack and heap memory. Boxing occurs implicitly when you assign a value type to an object variable, while unboxing requires explicit casting and can throw exceptions if the types don't match. Syntax Following is the syntax for boxing (implicit conversion) − object boxedValue = valueType; Following is the syntax for unboxing (explicit conversion) − ValueType unboxedValue = (ValueType)boxedObject; ...

Read More

DateTime.AddDays() Method in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 10K+ Views

The DateTime.AddDays() method in C# is used to add the specified number of days to the value of this instance. This method returns a new DateTime object without modifying the original instance, as DateTime is immutable. Syntax Following is the syntax − public DateTime AddDays(double days); Parameters days − A number of whole and fractional days. The value parameter can be negative or positive. Return Value This method returns a new DateTime object whose value is the sum of the date and time represented by this instance and the ...

Read More

Print number with commas as 1000 separators in C#

Giri Raju
Giri Raju
Updated on 17-Mar-2026 2K+ Views

In C#, you can format numbers with commas as thousand separators using various approaches. The most common and efficient methods involve using format strings with the ToString() method or composite formatting. Syntax Following is the syntax for formatting numbers with thousand separators − number.ToString("N") // Standard numeric format number.ToString("#, ##0.##") // Custom format with commas string.Format("{0:N}", number) // Composite formatting Using Standard Numeric Format ("N") The "N" format specifier automatically adds thousand separators and formats the number according to the current ...

Read More

C# Hexadecimal ("X") Format Specifier

Samual Sam
Samual Sam
Updated on 17-Mar-2026 9K+ Views

The hexadecimal ("X") format specifier is used to convert a number to a string of hexadecimal digits. The case of the format specifier determines whether hexadecimal digits greater than 9 are displayed in uppercase or lowercase. Use "X" for uppercase hexadecimal letters (A, B, C, D, E, F) and "x" for lowercase hexadecimal letters (a, b, c, d, e, f). Syntax Following is the syntax for using the hexadecimal format specifier − number.ToString("X") // uppercase number.ToString("x") // lowercase number.ToString("X4") // uppercase with minimum 4 ...

Read More

DateTime.AddHours() Method in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 7K+ Views

The DateTime.AddHours() method in C# is used to add the specified number of hours to the value of this instance. This method returns a new DateTime object, leaving the original instance unchanged. Syntax Following is the syntax − public DateTime AddHours(double hrs); Parameters hrs − A number of hours to be added. The value can be positive (to add hours) or negative (to subtract hours). Fractional values represent parts of an hour. Return Value Returns a new DateTime object that represents the date and time that results from adding the specified ...

Read More

How to compare two arrays in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

Comparing arrays in C# can be done using several approaches. The most common and efficient method is using the SequenceEqual() method from LINQ, but you can also compare arrays manually using loops or other techniques. Syntax Following is the syntax for using SequenceEqual() to compare two arrays − bool result = array1.SequenceEqual(array2); Following is the syntax for manual comparison using a loop − bool areEqual = true; for (int i = 0; i < array1.Length; i++) { if (array1[i] != array2[i]) { ...

Read More
Showing 8651–8660 of 25,466 articles
« Prev 1 864 865 866 867 868 2547 Next »
Advertisements