Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 9 of 151

What does the interface IStructuralEquatable do in C#?

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

The IStructuralEquatable interface in C# defines methods to support the comparison of objects for structural equality. This means two objects are considered equal if they have the same structure and equal values, rather than being the same reference. This interface is particularly useful for collections like arrays, tuples, and other composite objects where you want to compare the actual content rather than object references. Syntax The interface defines two methods − public interface IStructuralEquatable { bool Equals(object other, IEqualityComparer comparer); int GetHashCode(IEqualityComparer comparer); } Methods ...

Read More

C# program to print unique values from a list

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

In C#, you can extract unique values from a List using the Distinct() LINQ method. This method filters out duplicate elements and returns only the unique values, maintaining the order of their first occurrence. Syntax Following is the syntax for using Distinct() method − List uniqueList = originalList.Distinct().ToList(); The Distinct() method returns an IEnumerable, so we use ToList() to convert it back to a List. Using Distinct() with Integer List Here's how to remove duplicate integers from a list and display only unique values − using System; using System.Collections.Generic; using ...

Read More

C# Console.WindowHeight Property

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

The Console.WindowHeight property in C# gets or sets the height of the console window measured in rows. This property allows you to retrieve the current console window height or modify it programmatically to control the display area of your console application. Syntax Following is the syntax for getting the window height − int height = Console.WindowHeight; Following is the syntax for setting the window height − Console.WindowHeight = newHeight; Return Value The property returns an int value representing the height of the console window in rows. When setting, it ...

Read More

Scope of Variables in C#

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

The scope of a variable in C# determines the region of code where a variable can be accessed and used. Understanding variable scope is crucial for writing efficient and error-free programs. C# has several levels of variable scope, each with different accessibility rules and lifetimes. Types of Variable Scope Method Level (Local Variables) Variables declared inside a method are local variables. They are only accessible within that specific method and are destroyed when the method execution completes − using System; class Program { public void TestMethod() { ...

Read More

Final local variables in C#

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

In C#, there is no direct equivalent to Java's final keyword for local variables. However, you can achieve similar behavior using the readonly keyword for fields and implicit typing with var or const for local variables that should not change after initialization. The readonly keyword allows a field to be assigned a value only once − either at the time of declaration or in the constructor. Once assigned, it cannot be modified. Syntax Following is the syntax for declaring a readonly field − readonly dataType fieldName; For local variables that should remain constant, ...

Read More

Append to StringBuilder in C#

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

The Append() method in C# is used to add content to a StringBuilder object. Unlike strings, which are immutable, StringBuilder allows efficient concatenation of text without creating new string objects in memory. StringBuilder is particularly useful when you need to perform multiple string concatenations, as it provides better performance than using the + operator repeatedly. Syntax Following is the syntax for creating a StringBuilder and using the Append() method − StringBuilder sb = new StringBuilder(); sb.Append(value); The Append() method can accept various data types including string, char, int, double, and others. It returns ...

Read More

C# difference in milliseconds between two DateTime

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

In C# programming, you often need to calculate the time difference between two DateTime objects. The most efficient way to get the difference in milliseconds is by using the TimeSpan structure and its TotalMilliseconds property. Syntax Following is the syntax for calculating milliseconds difference between two DateTime objects − DateTime date1 = new DateTime(year, month, day, hour, minute, second); DateTime date2 = new DateTime(year, month, day, hour, minute, second); TimeSpan difference = date2 - date1; double milliseconds = difference.TotalMilliseconds; Using TimeSpan to Calculate Milliseconds Difference When you subtract one DateTime from another, the ...

Read More

Dictionary Methods in C#

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

Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collections.Generic namespace and provides methods to add, remove, and search for key-value pairs efficiently. Common Dictionary Methods Method Description Add(TKey, TValue) Adds a key-value pair to the Dictionary Remove(TKey) Removes the element with the specified key Clear() Removes all keys and values from the Dictionary ContainsKey(TKey) Checks whether the specified key exists ContainsValue(TValue) Checks whether the specified value exists TryGetValue(TKey, out TValue) Safely retrieves a value ...

Read More

How to find the average of elements of an integer array in C#?

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

Finding the average of elements in an integer array is a common programming task in C#. The average is calculated by dividing the sum of all elements by the total number of elements in the array. Syntax The basic approach involves three steps − int[] array = {element1, element2, element3, ...}; int sum = 0; for (int i = 0; i < array.Length; i++) { sum += array[i]; } int average = sum / array.Length; Using For Loop to Calculate Average The traditional approach uses a for loop to ...

Read More

Replace a string using StringBuilder

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

The StringBuilder class in C# provides an efficient way to modify strings without creating new string objects. The Replace() method allows you to replace all occurrences of a specified string or character with another string or character directly within the StringBuilder object. Syntax Following is the syntax for the Replace() method in StringBuilder − public StringBuilder Replace(string oldValue, string newValue) public StringBuilder Replace(char oldChar, char newChar) public StringBuilder Replace(string oldValue, string newValue, int startIndex, int count) Parameters oldValue/oldChar − The string or character to be replaced. newValue/newChar − The string or character ...

Read More
Showing 81–90 of 1,507 articles
« Prev 1 7 8 9 10 11 151 Next »
Advertisements