Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 11 of 151

Comments in C#

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

Comments in C# are used to document and explain code, making it more readable and maintainable. The C# compiler ignores all comments during compilation, so they don't affect the program's performance. C# supports three types of comments: single-line, multi-line, and XML documentation comments. Syntax Following is the syntax for single-line comments − // This is a single-line comment Following is the syntax for multi-line comments − /* This is a multi-line comment that spans multiple lines */ Following is the syntax for XML documentation comments − ...

Read More

How to find Volume and Surface Area of a Sphere using C#?

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

To find the volume and surface area of a sphere using C#, we need to apply mathematical formulas that use the sphere's radius. A sphere is a perfectly round three-dimensional object where every point on its surface is equidistant from its center. Sphere Formulas r Sphere Formulas Surface Area = 4πr² Volume = (4/3)πr³ where r = radius of the sphere Formulas The mathematical formulas for a sphere are − ...

Read More

Orderby clause in C#

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

The orderby clause in C# is used to sort elements in a collection based on one or more specified criteria. It supports both ascending (default) and descending order, and can be used with LINQ query expressions or method syntax. Syntax Following is the syntax for using orderby in query expression − var result = from element in collection orderby element.Property [ascending|descending] select element; Following is the syntax for ...

Read More

Why does the indexing start with zero in C# arrays?

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

In C#, array indexing starts at zero because arrays are fundamentally based on memory addressing and offset calculations. This design choice has deep roots in computer science and provides both efficiency and logical consistency. When an array is created, it occupies a contiguous block of memory. The array name acts as a pointer to the first element's memory address, and accessing elements involves calculating offsets from this base address. Memory Layout and Addressing Consider an integer array with 5 elements stored in memory. Since each int occupies 4 bytes, the memory layout would look like this − ...

Read More

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

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

A palindrome is a number or string that reads the same forward and backward. In C#, we can check if a number is a palindrome by reversing its digits and comparing it with the original number. Syntax Following is the syntax for reversing a string using Array.Reverse() method − char[] charArray = string.ToCharArray(); Array.Reverse(charArray); string reversedString = new string(charArray); Following is the syntax for comparing strings ignoring case − bool isEqual = string1.Equals(string2, StringComparison.OrdinalIgnoreCase); Using String Reversal Method The most straightforward approach is to convert the number to a ...

Read More

Convert.ToDateTime Method in C#

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

The Convert.ToDateTime method in C# converts a string representation of a date and time to a DateTime object. This method is commonly used when you need to parse date strings from user input, files, or external data sources into a usable DateTime format. The method supports various date formats and provides automatic parsing based on the current culture settings of your system. Syntax Following are the most commonly used overloads of Convert.ToDateTime − public static DateTime ToDateTime(string value) public static DateTime ToDateTime(string value, IFormatProvider provider) public static DateTime ToDateTime(object value) Parameters ...

Read More

How to use 'as' operator in C#?

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

The as operator in C# performs safe type conversions between compatible reference types. Unlike direct casting, the as operator returns null if the conversion fails instead of throwing an exception, making it ideal for checking type compatibility. The as operator can perform reference conversions, nullable conversions, and boxing conversions, but cannot perform user-defined conversions or value type conversions (except nullable types). Syntax Following is the syntax for using the as operator − TargetType variable = expression as TargetType; If the conversion succeeds, variable contains the converted value. If it fails, variable is null. ...

Read More

Find frequency of each word in a string in C#

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

Finding the frequency of each word in a string in C# involves splitting the string into individual words and counting their occurrences. This is a common text processing task useful for data analysis, word clouds, and text mining applications. Using Dictionary for Word Frequency The most efficient approach is to use a Dictionary to store words as keys and their frequencies as values − using System; using System.Collections.Generic; class WordFrequency { public static void Main() { string text = "apple banana apple orange banana apple"; ...

Read More

C# Program to invert the order of elements in a sequence

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

In C#, you can invert the order of elements in a sequence using several built-in methods. The most common approaches include using Queryable.Reverse() with LINQ, Array.Reverse(), and Enumerable.Reverse(). Each method has its specific use cases and performance characteristics. Syntax Following is the syntax for using Queryable.Reverse() method − IQueryable result = sequence.AsQueryable().Reverse(); Following is the syntax for using Array.Reverse() method − Array.Reverse(array); Following is the syntax for using Enumerable.Reverse() method − IEnumerable result = sequence.Reverse(); Using Queryable.Reverse() Method The Queryable.Reverse() method returns an IQueryable that ...

Read More

How to find minimum between 2 numbers using C#?

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

Finding the minimum between two numbers in C# can be accomplished using several approaches. The most common methods include using conditional statements like if-else or utilizing built-in methods like Math.Min(). Using if-else Statement The traditional approach uses an if-else statement to compare two numbers and determine the smaller value − using System; class Program { static void Main(string[] args) { int num1 = 50; int num2 = 90; int minNum; ...

Read More
Showing 101–110 of 1,507 articles
« Prev 1 9 10 11 12 13 151 Next »
Advertisements