Decimal.CompareTo() Method in C#

AmitDiwan
Updated on 17-Mar-2026 07:04:35

199 Views

The Decimal.CompareTo() method in C# is used to compare the current decimal instance with another decimal value or object. It returns an integer that indicates whether the current instance is less than, equal to, or greater than the comparison value. Syntax The Decimal.CompareTo() method has two overloads − public int CompareTo(decimal value); public int CompareTo(object value); Parameters value − A decimal number or object to compare with the current instance. Return Value The method returns an int value indicating the comparison result − Less than zero − ... Read More

C# program to Reverse words in a string

Samual Sam
Updated on 17-Mar-2026 07:04:35

3K+ Views

In C#, reversing words in a string means reversing each individual word while keeping the words in their original positions. For example, "Hello World" becomes "olleH dlroW" where each word is reversed but the word order remains the same. Syntax Using LINQ with Split(), Select(), and Reverse() methods − string result = string.Join(" ", str.Split(' ').Select(word => new string(word.Reverse().ToArray()))); Using a traditional loop approach − string[] words = str.Split(' '); for (int i = 0; i < words.Length; i++) { words[i] = ReverseWord(words[i]); } string result = string.Join(" ... Read More

What are the difference between Composition and Aggregation in C#?

Ankith Reddy
Updated on 17-Mar-2026 07:04:35

664 Views

Composition and Aggregation are two important types of associations in C# that represent different levels of dependency between classes. Understanding these relationships helps design better object-oriented systems with proper coupling and lifecycle management. Composition Composition represents a strong "part-of" relationship where the child object cannot exist independently of the parent object. When the parent is destroyed, all child objects are also destroyed automatically. Composition: Strong Ownership Car (Owner) Engine (Owned) ... Read More

How to join two lists in C#?

Ankith Reddy
Updated on 17-Mar-2026 07:04:35

6K+ Views

To join two lists in C#, you can use several methods. The most common approaches are using the AddRange() method to modify an existing list, or using LINQ's Concat() method to create a new combined list without modifying the originals. Syntax Using AddRange() to modify the first list − list1.AddRange(list2); Using LINQ Concat() to create a new list − var combinedList = list1.Concat(list2).ToList(); Using AddRange() Method The AddRange() method adds all elements from the second list to the end of the first list, modifying the original list − ... Read More

C# program to get the file name in C#

Chandu yadav
Updated on 17-Mar-2026 07:04:35

777 Views

In C#, you can extract the file name from a full path using the Path.GetFileName() method from the System.IO namespace. This method returns only the file name and extension, without the directory path. Syntax Following is the syntax for using Path.GetFileName() method − string fileName = Path.GetFileName(filePath); Parameters filePath − A string containing the full path to the file. Return Value The method returns a string containing the file name and extension. If the path ends with a directory separator, it returns an empty string. Using ... Read More

C# Linq ElementAt Method

karthikeya Boyini
Updated on 17-Mar-2026 07:04:35

374 Views

The ElementAt() method in C# LINQ returns the element at a specified index position in a sequence. It is part of the LINQ extension methods and can be used with any IEnumerable collection. The ElementAt() method throws an ArgumentOutOfRangeException if the index is out of bounds. For safer access, you can use ElementAtOrDefault(), which returns the default value for the type if the index is invalid. Syntax Following is the syntax for the ElementAt() method − public static TSource ElementAt(this IEnumerable source, int index) Following is the syntax for the ElementAtOrDefault() method − ... Read More

C# program to find IP Address of the client

karthikeya Boyini
Updated on 17-Mar-2026 07:04:35

2K+ Views

To find the IP address of the client machine in C#, we use the DNS (Domain Name System) class from the System.Net namespace. This involves getting the hostname first and then resolving it to obtain the associated IP addresses. Syntax Following is the syntax to get the hostname − string hostName = Dns.GetHostName(); Following is the syntax to get IP addresses from hostname − IPHostEntry hostEntry = Dns.GetHostEntry(hostName); IPAddress[] addresses = hostEntry.AddressList; How It Works The process involves two main steps: Dns.GetHostName() retrieves the hostname of the ... Read More

Environment.NewLine in C#

Arjun Thakur
Updated on 17-Mar-2026 07:04:35

2K+ Views

The Environment.NewLine property in C# provides a platform-independent way to add line breaks in strings. It automatically returns the appropriate newline character sequence for the current operating system − \r on Windows and on Unix-based systems. Using Environment.NewLine ensures your code works correctly across different platforms without hardcoding specific line break characters. Syntax Following is the syntax for using Environment.NewLine − string result = text1 + Environment.NewLine + text2; You can also use it in string concatenation or interpolation − string result = $"Line 1{Environment.NewLine}Line 2"; Basic Usage ... Read More

Sortable ("s") Format Specifier in C#

Arjun Thakur
Updated on 17-Mar-2026 07:04:35

567 Views

The Sortable ("s") format specifier in C# represents a standardized date and time format that produces ISO 8601-compliant strings. This format is particularly useful for sorting dates chronologically as strings and for data interchange between different systems. The "s" format specifier is defined by the DateTimeFormatInfo.SortableDateTimePattern property and follows a consistent pattern regardless of the current culture settings. Syntax The sortable format specifier uses the following syntax − DateTime.ToString("s") This corresponds to the custom format pattern − yyyy'-'MM'-'dd'T'HH':'mm':'ss Format Pattern Breakdown Sortable Format Pattern: ... Read More

Decimal.Divide() Method in C#

AmitDiwan
Updated on 17-Mar-2026 07:04:35

2K+ Views

The Decimal.Divide() method in C# is used to divide two specified Decimal values and return the quotient. This static method provides precise division operations for financial and monetary calculations where accuracy is critical. Syntax Following is the syntax − public static decimal Divide(decimal val1, decimal val2); Parameters val1 − The dividend (the number to be divided). val2 − The divisor (the number by which val1 is divided). Return Value Returns a decimal value representing the result of dividing val1 by val2. The method throws a ... Read More

Advertisements