Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Samual Sam
Page 8 of 151
How to copy or clone a C# list?
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 Morechar vs string keywords in C#
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 MoreWhat is compile time polymorphism in C#?
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 MoreImplicit conversion from 64-bit signed integer (long) to Decimal in C#
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 MoreCompressing and Decompressing files in C#
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 MoreWhat is late binding in C#?
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 MoreC# program to check if two lists have at-least one element common
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 MoreWhat is the difference between implicit and explicit type conversion in C#?
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 MoreC# Program to determine the difference in hours between two dates
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 MoreCall a method Asynchronously in C#
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