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 813 of 2547
Three Different ways to calculate factorial in C#
The factorial of a number is the product of all positive integers less than or equal to that number. For example, factorial of 5 (written as 5!) is 5 × 4 × 3 × 2 × 1 = 120. In C#, you can calculate factorial using three different approaches − Syntax The mathematical definition of factorial is − n! = n × (n-1) × (n-2) × ... × 2 × 1 0! = 1 (by definition) 1! = 1 Factorial Calculation Methods For Loop ...
Read MoreC# 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 MoreConvert.ToDouble Method in C#
The Convert.ToDouble() method in C# converts a specified value to a double-precision floating-point number. This method is part of the Convert class and can handle various data types including integers, strings, booleans, and other numeric types. Syntax Following is the syntax for the Convert.ToDouble() method − public static double ToDouble(object value) public static double ToDouble(string value) public static double ToDouble(int value) public static double ToDouble(long value) public static double ToDouble(bool value) Parameters value − The value to be converted to a double-precision floating-point number. This can be of various types including object, string, ...
Read Moretry keyword in C#
The try keyword in C# is used to define a block of code that might throw an exception. It must be followed by one or more catch blocks to handle exceptions, and optionally a finally block for cleanup code that runs regardless of whether an exception occurs. Syntax Following is the basic syntax for a try-catch block − try { // code that might throw an exception } catch (ExceptionType e) { // exception handling code } Following is the syntax with a finally block − ...
Read MoreC# TicksPer constants
The TicksPer constants in C# are predefined values in the TimeSpan class that represent the number of ticks for different time units. A tick is the smallest unit of time measurement in .NET, where one tick equals 100 nanoseconds. These constants help convert between ticks and common time measurements. Available TicksPer Constants The TimeSpan class provides the following TicksPer constants − TicksPerDay − Number of ticks in one day TicksPerHour − Number of ticks in one hour TicksPerMinute − Number of ticks in one minute TicksPerSecond − Number of ticks in one second TicksPerMillisecond − Number ...
Read MoreContainsKey() method in C#
The ContainsKey() method in C# is used to check whether a specific key exists in a Hashtable or other collection types like Dictionary. This method returns true if the key is found, otherwise it returns false. Syntax Following is the syntax for the ContainsKey() method − bool ContainsKey(object key) Parameters key − The key to locate in the collection. Return Value Returns true if the key exists in the collection; otherwise, false. ContainsKey() Method Logic Hashtable ...
Read MoreHow to use StringBuilder in C#?
The StringBuilder class in C# is designed for efficient string manipulation when you need to perform multiple operations like appending, inserting, or replacing characters. Unlike regular strings, which are immutable and create new objects for every modification, StringBuilder maintains a mutable buffer that can be expanded without creating new objects in memory. Syntax Following is the syntax to initialize a StringBuilder − StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(string value); StringBuilder sb = new StringBuilder(int capacity); StringBuilder sb = new StringBuilder(string value, int capacity); Key Advantages of StringBuilder Mutable ...
Read MoreTimer in C#
The System.Timers.Timer class in C# generates events at specified intervals. It is useful for executing code periodically, such as updating displays, checking status, or performing background tasks. The timer can be configured to fire once or repeatedly, and provides precise control over timing intervals and event handling. Syntax Following is the syntax for creating and configuring a timer − Timer timer = new System.Timers.Timer(intervalInMilliseconds); timer.Elapsed += EventHandlerMethod; timer.Enabled = true; Following is the syntax for the event handler method − private static void EventHandlerMethod(Object source, ElapsedEventArgs e) { ...
Read MoreRepresent exact representation of no time in C#
The TimeSpan.Zero property in C# represents the exact representation of no time, which displays as 00:00:00. This is a static readonly field that provides a convenient way to initialize or compare TimeSpan values when you need to represent zero duration. Syntax Following is the syntax for using TimeSpan.Zero − TimeSpan timespan = TimeSpan.Zero; You can also use it for comparison operations − if (timespan == TimeSpan.Zero) { // timespan represents no time } Using TimeSpan.Zero for Initialization The most common use of TimeSpan.Zero is to initialize ...
Read MoreClear a Hashtable in C#
The Clear() method in C# is used to remove all key-value pairs from a Hashtable. This method provides an efficient way to empty the entire Hashtable in a single operation, resetting its count to zero while maintaining the original capacity. Syntax Following is the syntax for the Clear()
Read More