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 George John
Page 6 of 79
C# Program to Add Two TimeSpan
The TimeSpan structure in C# represents a time interval and provides methods to perform arithmetic operations. You can add two TimeSpan objects using the Add() method or the + operator to get the combined duration. Syntax Following is the syntax for adding two TimeSpan objects using the Add() method − TimeSpan result = timespan1.Add(timespan2); You can also use the + operator for addition − TimeSpan result = timespan1 + timespan2; Using the Add() Method The Add() method returns a new TimeSpan that represents the sum of two time intervals ...
Read MoreStopwatch class in C#
The Stopwatch class in C# is used to measure elapsed time with high precision. It provides accurate timing measurements for performance monitoring and benchmarking applications. The class is found in the System.Diagnostics namespace. Syntax Following is the syntax for creating and starting a Stopwatch − Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Alternatively, you can create and start in one line − Stopwatch stopwatch = Stopwatch.StartNew(); Key Properties and Methods Property/Method Description Start() Starts measuring elapsed time Stop() Stops measuring elapsed ...
Read MoreGet first three letters from every string in C#
In C#, you can extract the first three letters from every string using the Substring() method combined with LINQ operations. This is useful for creating abbreviations, prefixes, or shortened versions of strings in a collection. Syntax Following is the syntax for getting a substring from a string − string.Substring(startIndex, length) Following is the syntax for applying substring to a collection using LINQ − collection.Select(str => str.Substring(0, 3)) Using Substring with LINQ Select The most common approach is to use the Substring() method with LINQ's Select() method to transform each ...
Read MoreHow do you find the length of an array in C#?
To find the length of an array in C#, use the Length property (not a method). The Length property returns the total number of elements in the array as an integer value. Syntax Following is the syntax for using the Length property − int length = arrayName.Length; Parameters arrayName − The array whose length you want to find Return Value The Length property returns an int value representing the total number of elements in the array. Example Let us see an example − using System; ...
Read MoreUlong type in C#
The ulong type in C# is a 64-bit unsigned integer that represents the System.UInt64 structure. It can store values from 0 to 18, 446, 744, 073, 709, 551, 615, making it ideal for storing large positive numbers without sign considerations. The ulong keyword is an alias for System.UInt64 and is part of C#'s built-in value types. Since it's unsigned, it cannot store negative values but has twice the positive range compared to long. Syntax Following is the syntax for declaring a ulong variable − ulong variableName = value; You can also use the ...
Read MoreKeyValuePair in C#
The KeyValuePair struct in C# stores a pair of values in a single object, where one value acts as the key and the other as the value. It is commonly used in collections like List and as the element type in dictionaries. KeyValuePair is particularly useful when you need to associate two related pieces of data together without creating a custom class or when working with dictionary enumerations. Syntax Following is the syntax for creating a KeyValuePair − KeyValuePair pair = new KeyValuePair(key, value); Following is the syntax for accessing key and value ...
Read MoreWhat is the difference between overriding and shadowing in C#?
In C#, overriding and shadowing (also known as method hiding) are two different mechanisms for modifying inherited behavior. Overriding uses the virtual/override keywords to provide specific implementations of base class methods, while shadowing uses the new keyword to hide base class members entirely. Syntax Following is the syntax for method overriding − // Base class public virtual void MethodName() { } // Derived class public override void MethodName() { } Following is the syntax for method shadowing/hiding − // Base class public void MethodName() { } // Derived class public ...
Read MoreC# orderby Keyword
The orderby keyword in C# is used to sort data in LINQ queries. It allows you to sort collections in either ascending or descending order based on one or more criteria. Syntax Following is the syntax for using orderby in LINQ queries − from element in collection orderby element ascending select element; For descending order − from element in collection orderby element descending select element; Multiple sorting criteria can be applied using commas − from element in collection orderby element.Property1, element.Property2 descending select element; Using orderby ...
Read MoreRepresent Int32 as a Octal String in C#
To represent Int32 as an octal string in C#, use the Convert.ToString() method with base 8 as the second parameter. An Int32 represents a 32-bit signed integer that can hold values from -2, 147, 483, 648 to 2, 147, 483, 647. Syntax Following is the syntax for converting an Int32 to octal string − Convert.ToString(intValue, 8) Parameters intValue − The integer value to convert. 8 − The base for octal representation. Return Value The method returns a string representing the integer in octal format (base ...
Read MoreHow to use the Main() method in C#?
The Main() method in C# is the entry point of any C# application. It is a static method that runs when the program starts, without requiring an instance of the class to be created. The Main() method defines what the class does when executed and can instantiate other objects and variables. Syntax Following are the valid signatures for the Main() method − static void Main() static void Main(string[] args) static int Main() static int Main(string[] args) Parameters The Main() method signature includes the following components − static − The method ...
Read More