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
Programming Articles
Page 97 of 2547
How 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 MoreDisplay the default if element is not found in a C# List
When working with C# Lists, attempting to access an element that doesn't exist can throw exceptions. To safely handle empty lists or missing elements, you can use methods like FirstOrDefault() which return the default value for the type instead of throwing an exception. For reference types like strings, the default value is null. For value types like int, float, or double, the default value is 0. For bool, it's false. Syntax Following is the syntax for using FirstOrDefault() on a List − list.FirstOrDefault(); list.FirstOrDefault(condition); You can also specify a custom default value using ...
Read MoreDateTime.FromFileTimeUtc() Method in C#
The DateTime.FromFileTimeUtc() method in C# converts a Windows file time to an equivalent UTC DateTime. This method is useful when working with file timestamps or system-level operations that use Windows file time format, which represents time as the number of 100-nanosecond intervals since January 1, 1601 UTC. Syntax Following is the syntax − public static DateTime FromFileTimeUtc(long fileTime); Parameters The method accepts the following parameter − fileTime − A Windows file time expressed in ticks (100-nanosecond intervals since January 1, 1601 UTC). Return Value Returns a DateTime object ...
Read MoreTrigonometric Functions in C#
Trigonometric functions in C# are part of the Math class in the System namespace. These functions enable you to perform mathematical calculations involving angles, including basic trigonometric operations (Sin, Cos, Tan) and their inverse functions (Asin, Acos, Atan). All angle measurements in C# trigonometric functions are in radians, not degrees. To convert degrees to radians, multiply by π/180. Common Trigonometric Functions Function Description Return Type Math.Sin(double) Returns the sine of the specified angle double Math.Cos(double) Returns the cosine of the specified angle double Math.Tan(double) Returns the ...
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 MoreHow to find the size of a list in C#?
A List in C# is a dynamic collection that can grow or shrink in size. To determine the size of a list, you can use two different properties: Count (for the number of actual elements) and Capacity (for the total allocated space). Syntax To get the number of elements in a list − listName.Count To get the total capacity (allocated space) of a list − listName.Capacity Understanding Count vs Capacity Count vs Capacity Count Number of actual ...
Read MoreRead in a file in C# with StreamReader
The StreamReader class in C# is used to read text files efficiently. It provides methods to read characters, lines, or the entire content of a text file. StreamReader is part of the System.IO namespace and implements IDisposable, making it suitable for use with using statements for automatic resource cleanup. Syntax Following is the syntax to create a StreamReader object − StreamReader sr = new StreamReader("filename.txt"); Following is the syntax to use StreamReader with automatic disposal − using (StreamReader sr = new StreamReader("filename.txt")) { // read operations } ...
Read MoreDifference between TimeSpan Seconds() and TotalSeconds()
The TimeSpan.Seconds property returns only the seconds component of a time span, whereas TimeSpan.TotalSeconds property converts the entire time duration into seconds. Understanding this difference is crucial when working with time calculations in C#. Syntax Following is the syntax for accessing the seconds component − TimeSpan ts = new TimeSpan(days, hours, minutes, seconds, milliseconds); int seconds = ts.Seconds; Following is the syntax for getting total seconds − TimeSpan ts = new TimeSpan(days, hours, minutes, seconds, milliseconds); double totalSeconds = ts.TotalSeconds; How It Works TimeSpan: 1 ...
Read MoreUri.MakeRelativeUri(Uri) Method in C#
The Uri.MakeRelativeUri(Uri) method in C# is used to determine the relative difference between two Uri instances. It returns a relative URI that, when resolved against the base URI, yields the target URI. Syntax Following is the syntax − public Uri MakeRelativeUri(Uri uri); Parameters uri − The URI to compare to the current URI instance. This represents the target URI for which you want to create a relative path. Return Value Returns a Uri object that represents the relative URI from the current instance to the specified URI. ...
Read More