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 11 of 151
Comments in C#
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 MoreHow to find Volume and Surface Area of a Sphere using C#?
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 MoreOrderby clause in C#
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 MoreWhy does the indexing start with zero in C# arrays?
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 MoreWrite a C# program to check if a number is Palindrome or not
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 MoreConvert.ToDateTime Method in C#
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 MoreHow to use 'as' operator in C#?
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 MoreFind frequency of each word in a string in C#
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 MoreC# Program to invert the order of elements in a sequence
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 MoreHow to find minimum between 2 numbers using C#?
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