Leap Year Program in C#: IsLeapYear and the if/else Rule

Leap year program in C#: DateTime.IsLeapYear output showing 1900 False, 2000 True, 2024 True, 2026 False and 2028 True

February 29, 2028 is the next leap day. A leap year program in C# needs one call, DateTime.IsLeapYear(year), or one expression: (year % 4 == 0 && year % 100 != 0) || year % 400 == 0. In this tutorial, I’ll show the built-in method, the manual leap year program in C# with if/else, … Read more >>

How to Format a Number with Commas and Decimals in C#

C# format number with commas: 9876543.219 formatted with ToString("N2") as 9,876,543.22

Need 1234567 to print as 1,234,567? To format a number with commas in C#, call number.ToString(“N0”) for whole numbers or ToString(“N2”) for two decimals, and pass an en-US culture so the separator doesn’t change from machine to machine. In this tutorial, I’ll cover each case I’ve hit in production code for C# format number with … Read more >>

How to Reverse an Array in C# (8 Methods with Examples)

Reverse an array in C#: TX, CA, NY, FL becomes FL, NY, CA, TX with Array.Reverse(states)

Your sales report lists January first, but the dashboard wants the newest month on top. To reverse an array in C#, call Array.Reverse(arr). It flips the array in place in one line. In this tutorial, I’ll walk through eight ways to reverse an array in C#, from the built-in method to loops that skip Reverse() … Read more >>

C# String Array Contains: Check if an Array Contains a Value

C# string array contains: Contains() code and True/False output

Got a list of states and need to know whether “Texas” is in it? In C#, a string array contains check is one line: states.Contains(“Texas”) returns true or false. In this tutorial, I’ll walk you through every flavor of the C# string array contains question: exact match, ignore case, substring, StartsWith, wildcard, null elements, multiple … Read more >>

How to Convert a String to Int Without Parse in C#

Convert string to int without parse in C#: the string "4815" becomes the int 4815 through a c - '0' digit loop

Interviewers love this one: turn “4815” into an int without calling int.Parse. To convert a string to int without Parse in C#, loop over the characters and build the number yourself: In this tutorial, I’ll write my own convert string to int without parse in C# routine, then add a sign, invalid-character checks and overflow … Read more >>

How to Reverse a String in C# (7 Methods with Examples)

Reverse a string in C#: Albuquerque becomes euqreuqublA with Array.Reverse

Need to turn “Albuquerque” into “euqreuqublA”? To reverse a string in C#, copy it with ToCharArray(), flip the copy with Array.Reverse(), and wrap it in new string(…). C# strings don’t have a built-in Reverse() method, so that three-step pattern is the standard answer. In this tutorial, I’ll cover seven ways to reverse a string in … Read more >>

Prime Number Program in C#: Check, List and Sieve Primes

Prime number program in C#: boxes 2 to 13 with the primes 2, 3, 5, 7, 11 and 13 highlighted, and the count of 25 primes from 1 to 100

Is 97 prime? A prime number program in C# answers that by trying divisors: return false below 2, return n == 2 for even numbers, then test odd divisors while i <= n / i. If none divides evenly, it’s prime. In this tutorial, I’ll grow one small PrimeTools class step by step, the way … Read more >>