George John

George John

789 Articles Published

Articles by George John

Page 3 of 79

C# Fixed-Point ("F") Format Specifier

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

The Fixed-Point ("F") format specifier in C# converts a number to a string with a fixed number of decimal places. It produces output in the form "-ddd.ddd..." where "d" represents a digit (0-9). The negative sign appears only for negative numbers. Syntax Following is the syntax for using the Fixed-Point format specifier − number.ToString("F") // Default 2 decimal places number.ToString("Fn") // n decimal places (0-99) number.ToString("F", culture) // With specific culture Default Precision When no precision ...

Read More

C# program to remove all duplicates words from a given sentence

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

Removing duplicate words from a sentence is a common string manipulation task in C#. This process involves splitting the sentence into individual words, identifying duplicates, and keeping only unique words while preserving the original structure. There are several approaches to accomplish this task, ranging from using LINQ's Distinct() method to using collections like HashSet for efficient duplicate removal. Using LINQ Distinct() Method The Distinct() method from LINQ provides a straightforward way to remove duplicates from a collection − using System; using System.Linq; public class Program { public static void Main() { ...

Read More

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 211 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 551 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 179 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
Showing 21–30 of 789 articles
« Prev 1 2 3 4 5 79 Next »
Advertisements