Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 8 of 151

How to copy or clone a C# list?

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

Copying or cloning a C# list means creating a duplicate of the original list. There are several approaches to accomplish this, each suitable for different scenarios. The choice depends on whether you need a shallow copy or deep copy, and the target data structure. Syntax Following are the common syntaxes for copying a list − // Using constructor List newList = new List(originalList); // Using ToList() method List newList = originalList.ToList(); // Using CopyTo() method T[] array = new T[originalList.Count]; originalList.CopyTo(array); Using List Constructor The most straightforward way to clone a ...

Read More

char vs string keywords in C#

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

The char and string keywords in C# are used to work with textual data, but they serve different purposes. The char keyword represents a single character, while string represents a sequence of characters. Understanding the difference between these two data types is essential for effective text manipulation in C#. Syntax Following is the syntax for declaring a char variable − char variableName = 'A'; // Single quotes for character Following is the syntax for declaring a string variable − string variableName = "Hello"; // Double quotes for string ...

Read More

What is compile time polymorphism in C#?

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

Compile-time polymorphism in C# is a type of polymorphism where the method to be called is determined during compilation rather than at runtime. It is also known as static polymorphism or early binding, because the method binding occurs at compile time. C# provides two main techniques to implement compile-time polymorphism − Method Overloading and Operator Overloading. The compiler resolves which method to call based on the method signature (method name, number of parameters, and parameter types). Compile-time Polymorphism Method Resolution at Compile Time Compiler determines which method ...

Read More

Implicit conversion from 64-bit signed integer (long) to Decimal in C#

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

The long type represents a 64-bit signed integer in C#. C# allows implicit conversion from long to decimal because no data loss occurs during this conversion. The decimal type can accommodate all possible long values while providing higher precision. Syntax Following is the syntax for implicit conversion from long to decimal − long longValue = 123456789; decimal decimalValue = longValue; // Implicit conversion How It Works The conversion happens automatically because the decimal type has a wider range and higher precision than long. The decimal type can represent all integer values that ...

Read More

Compressing and Decompressing files in C#

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

File compression and decompression in C# is accomplished using the System.IO.Compression namespace. This namespace provides classes like GZipStream and ZipArchive for handling compressed files efficiently. Syntax Following is the syntax for compressing files using GZipStream − using (var compressStream = new GZipStream(outputStream, CompressionMode.Compress)) { // write data to compress } Following is the syntax for decompressing files using GZipStream − using (var decompressStream = new GZipStream(inputStream, CompressionMode.Decompress)) { // read decompressed data } Using GZipStream for File Compression To compress a ...

Read More

What is late binding in C#?

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

Late binding in C# refers to the process where the decision of which method to call is made at runtime rather than compile time. This is also known as dynamic polymorphism, where the actual method implementation is determined by the object type at runtime, not by the reference type. Late binding is implemented using virtual methods in base classes and override methods in derived classes. When a virtual method is called through a base class reference, the runtime determines which derived class method to execute based on the actual object type. How Late Binding Works In late ...

Read More

C# program to check if two lists have at-least one element common

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

A common requirement in C# programming is to check if two lists share at least one common element. This can be achieved using various approaches, from simple nested loops to more efficient methods using HashSets and LINQ. The most efficient approach is to use HashSet operations or LINQ's Intersect() method, which provide optimal performance for finding common elements between collections. Using HashSet Intersection HashSet provides an Overlaps() method that efficiently checks if two sets have any common elements − using System; using System.Collections.Generic; public class Program { public static void ...

Read More

What is the difference between implicit and explicit type conversion in C#?

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

Type conversion in C# refers to converting a value from one data type to another. There are two main types of conversions: implicit (automatic) and explicit (manual). Understanding when each occurs helps prevent data loss and compilation errors. Implicit Type Conversion Implicit conversions are performed automatically by the C# compiler when converting from a smaller data type to a larger one. This is safe because no data is lost in the process. Syntax smallerType variable = value; largerType newVariable = variable; // Automatic conversion Implicit Conversion Flow ...

Read More

C# Program to determine the difference in hours between two dates

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

In C#, you can calculate the difference in hours between two dates using the DateTime structure and the TimeSpan class. The TimeSpan represents a time interval and provides properties to get the difference in various units including hours. Syntax Following is the syntax for calculating the time difference − 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 hours = difference.TotalHours; Using DateTime Subtraction The most straightforward way to find the difference in hours is ...

Read More

Call a method Asynchronously in C#

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

Asynchronous programming in C# is an efficient approach for handling operations that may be blocked or delayed. In synchronous processing, if an operation is blocked, the entire application waits, causing it to become unresponsive and take longer to complete tasks. Using the asynchronous approach, applications can continue executing other tasks while waiting for blocked operations to complete. This is especially important in GUI applications where blocking the main thread causes the interface to freeze. The Problem with Synchronous Code In GUI applications, when synchronous operations take too long, the application shows "not responding" messages because the main ...

Read More
Showing 71–80 of 1,507 articles
« Prev 1 6 7 8 9 10 151 Next »
Advertisements