Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 12 of 151

Implicit conversion from Int16 to Decimal in C#

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

The short type in C# represents a 16-bit signed integer (Int16) that can store values from -32, 768 to 32, 767. C# allows implicit conversion from short to decimal because this conversion is always safe and will never result in data loss. An implicit conversion means the compiler automatically performs the type conversion without requiring an explicit cast operator. This is possible because decimal has a much larger range and precision than short. Syntax The syntax for implicit conversion from Int16 to Decimal is straightforward − short shortValue = value; decimal decimalValue = shortValue; ...

Read More

Write a C# program to check if a number is prime or not

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

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a number is prime in C#, we count how many divisors it has by iterating through all numbers from 1 to the number itself. The basic approach uses a counter that increments each time we find a divisor. If the counter equals 2 at the end (divisible only by 1 and itself), the number is prime. Algorithm The algorithm for checking prime numbers follows these steps − Initialize a counter to 0 ...

Read More

How to use RightShift Operators in C#?

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

The right shift operator (>>) in C# moves the bits of the left operand to the right by the number of positions specified by the right operand. This operation effectively divides the number by powers of 2. Syntax Following is the syntax for the right shift operator − result = operand >> numberOfPositions; Where operand is the value whose bits will be shifted, and numberOfPositions specifies how many positions to shift right. How Right Shift Works The right shift operator moves each bit to the right by the specified number of positions. ...

Read More

C# Program to find the average of a sequence of numeric values

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

Use the LINQ Average() method to find the average of a sequence of numeric values. LINQ provides multiple approaches to calculate averages from collections like arrays, lists, and other enumerable sequences. Syntax Following is the syntax for using the Average() method − // For IEnumerable collection.Average() // For IQueryable Queryable.Average(collection.AsQueryable()) Return Value The Average() method returns a double value representing the arithmetic mean of the sequence. For integer sequences, the result is automatically converted to double to preserve decimal precision. Using Average() with List Collections The simplest approach is to ...

Read More

Streams and Byte Streams in C#

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

A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream. Streams in C# provide a unified interface for reading and writing data, whether it's from files, memory, or network connections. The type of streams includes − Byte Streams − They handle raw binary data and include Stream, FileStream, MemoryStream, and BufferedStream. Character Streams − They handle text data and include TextReader, TextWriter, StreamReader, StreamWriter, and other text-based streams. Byte ...

Read More

C# program to convert floating to binary

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

Converting a floating-point number to binary representation in C# involves separating the number into its integer and fractional parts, then converting each part separately using different algorithms. The integer part is converted using repeated division by 2, while the fractional part is converted using repeated multiplication by 2. Algorithm The conversion process follows these steps − Separate the floating-point number into integer and fractional parts Convert the integer part by repeatedly dividing by 2 and collecting remainders Convert the fractional part by repeatedly multiplying by 2 and collecting integer parts Combine both parts with a ...

Read More

What is the Length property of BitArray class in C#?

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

The Length property of the BitArray class in C# is used to get or set the number of elements in the BitArray. This property allows you to determine the size of the bit array and also resize it when needed. Syntax Following is the syntax for using the Length property − public int Length { get; set; } Return Value The Length property returns an int value representing the number of elements in the BitArray. Using Length Property to Get BitArray Size You can use the Length property to determine how ...

Read More

How to concatenate Two Arrays in C#?

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

To concatenate two arrays in C#, you can use several methods depending on your requirements. The most common approaches include using the Concat() method from LINQ, copying elements manually, or using Array.Copy(). Syntax Using LINQ Concat() method − var result = array1.Concat(array2).ToArray(); Using Array.Copy() method − Array.Copy(sourceArray, destinationArray, length); Using LINQ Concat() Method The Concat() method from LINQ is the most straightforward way to concatenate arrays. It creates a new array containing elements from both arrays − using System; using System.Linq; class Program { ...

Read More

Implicit conversion from UInt64 to Decimal in C#

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

The ulong type represents a 64-bit unsigned integer (UInt64) that can store values from 0 to 18, 446, 744, 073, 709, 551, 615. C# supports implicit conversion from ulong to decimal, meaning the conversion happens automatically without requiring explicit casting. This conversion is safe because the decimal type can represent all possible ulong values without data loss, though the internal representation changes from integer to floating-point decimal format. Syntax Following is the syntax for implicit conversion from ulong to decimal − ulong ulongValue = value; decimal decimalValue = ulongValue; // implicit conversion ...

Read More

Socket Programming in C#

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

Socket programming in C# enables network communication between applications using the System.Net.Sockets namespace. This namespace provides a managed implementation of the Windows Sockets interface for creating client-server applications that communicate over TCP/IP networks. Socket programming has two basic modes − synchronous (blocking) and asynchronous (non-blocking) operations. Socket Class Constructor Syntax Following is the syntax for creating a Socket instance − Socket socket = new Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType); Parameters AddressFamily − Specifies the addressing scheme (InterNetwork for IPv4, InterNetworkV6 for IPv6) SocketType − Defines the type ...

Read More
Showing 111–120 of 1,507 articles
« Prev 1 10 11 12 13 14 151 Next »
Advertisements