George John

George John

789 Articles Published

Articles by George John

Page 2 of 79

Calculate minutes between two dates in C#

George John
George John
Updated on 17-Mar-2026 21K+ Views

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 More

Delete nth element from headnode using C#

George John
George John
Updated on 17-Mar-2026 188 Views

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 More

What are the differences between a list collection and an array in C#?

George John
George John
Updated on 17-Mar-2026 541 Views

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 More

Generate current month in C#

George John
George John
Updated on 17-Mar-2026 11K+ Views

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 More

How to find the first character of a string in C#?

George John
George John
Updated on 17-Mar-2026 45K+ Views

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 More

Display the default if element is not found in a C# List

George John
George John
Updated on 17-Mar-2026 165 Views

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 More

How to capture index out of range exception in C#?

George John
George John
Updated on 17-Mar-2026 2K+ Views

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 More

Different Star Pattern Programs in C#

George John
George John
Updated on 17-Mar-2026 27K+ Views

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 More

Difference between Method and Function in C#

George John
George John
Updated on 17-Mar-2026 7K+ Views

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 More

How to control for loop using break and continue statements in C#?

George John
George John
Updated on 17-Mar-2026 276 Views

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
Showing 11–20 of 789 articles
« Prev 1 2 3 4 5 79 Next »
Advertisements