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 2 of 79
Calculate minutes between two dates in C#
Calculating the difference in minutes between two dates is a common requirement in C# applications. The DateTime structure and TimeSpan class provide built-in functionality to perform this calculation efficiently. Syntax Following is the basic syntax for calculating minutes between two dates − DateTime date1 = new DateTime(year, month, day, hour, minute, second); DateTime date2 = new DateTime(year, month, day, hour, minute, second); TimeSpan difference = date2 - date1; double minutes = difference.TotalMinutes; How It Works When you subtract one DateTime from another, C# returns a TimeSpan object that represents the time difference. The ...
Read MoreDelete nth element from headnode using C#
Deleting the nth element from a linked list involves traversing to the target node and adjusting the links to bypass it. When deleting from the head position (n=1), we simply update the head reference. For other positions, we need to find the node before the target and redirect its Next pointer. Syntax Following is the basic structure for deleting the nth node − if (n == 1) { head = head.Next; return; } Node current = head; for (int i = 1; i < n - 1; ...
Read MoreWhat are the differences between a list collection and an array in C#?
List collection is a generic class that can store any data type to create a dynamic collection. Arrays, on the other hand, store a fixed-size sequential collection of elements of the same type. Understanding the differences between these two collection types is crucial for choosing the right approach for your application. Syntax Following is the syntax for declaring and initializing a List − List listName = new List(); Following is the syntax for declaring and initializing an array − dataType[] arrayName = new dataType[size]; dataType[] arrayName = {value1, value2, value3}; ...
Read MoreGenerate current month in C#
To generate the current month in C#, you can use the DateTime.Now property to get the current date and time, then extract the month information using various properties and formatting methods. Syntax Following is the syntax to get the current date − DateTime dt = DateTime.Now; Following is the syntax to get the month as a number − int month = dt.Month; Following is the syntax to get the month name using formatting − string monthName = dt.ToString("MMMM"); Getting Current Month as Number The Month ...
Read MoreHow to find the first character of a string in C#?
To get the first character of a string in C#, you can use several approaches. The most common methods are using string indexing, the Substring() method, or the First() LINQ method. Syntax Following are the different syntaxes to get the first character − char firstChar = str[0]; string firstChar = str.Substring(0, 1); char firstChar = str.First(); Using String Indexing The simplest way to get the first character is using string indexing with [0]. This returns a char type − using System; public class Demo ...
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 MoreHow to capture index out of range exception in C#?
The IndexOutOfRangeException occurs when you try to access an array element with an index that is outside the bounds of the array. This is a common runtime exception in C# that can be captured using try-catch blocks. Understanding Array Bounds Arrays in C# are zero-indexed, meaning the first element is at index 0. If an array has 5 elements, the valid indices are 0, 1, 2, 3, and 4. Accessing index 5 or higher will throw an IndexOutOfRangeException. Array Index Bounds ...
Read MoreDifferent Star Pattern Programs in C#
Star pattern programs are common programming exercises that help developers practice loops and understand nested iteration logic. C# provides simple syntax to create various star patterns using for loops and Console.Write() methods. Common Star Pattern Types Right Triangle * ** Pyramid * *** Inverted *** ** Diamond * *** Right Triangle Pattern This pattern creates a right-angled triangle where each row contains one more star than the previous row − using System; class Program { static void Main() { for (int i = 1; i
Read MoreDifference between Method and Function in C#
In C#, the terms method and function are often used interchangeably, but there is a subtle distinction. A method is a function that belongs to a class or struct, while a function is a more general term for a reusable block of code that performs a specific task. Every C# program has at least one class with a method named Main. Methods are defined within classes and operate on the data and behavior of those classes. Syntax Following is the syntax for defining a method in C# − [access modifier] [return type] MethodName(parameters) { ...
Read MoreHow to control for loop using break and continue statements in C#?
The break and continue statements provide powerful control flow mechanisms within for loops in C#. The break statement terminates the loop entirely, while the continue statement skips the current iteration and moves to the next one. Syntax Following is the syntax for using break statement in a for loop − for (initialization; condition; increment) { if (someCondition) { break; // exits the loop completely } // other statements } Following is the syntax for using continue statement in a ...
Read More