C# String Format for DateTime Overview

In this tutorial, we are going to use the method String.Format to format string for DateTime DataType. Format a String for DateTime using C# Example: DateTime dateTime = DateTime.Now; String date; date = String.Format("Date & Time Format: {0}", dateTime); Console.WriteLine(date); date = String.Format("Date Only Format: {0:D}", dateTime); Console.WriteLine(date); date = String.Format("Time Only Format: {0:T}", dateTime);... » read more

String.Format in C# for Decimal

In this article, we will learn how to use the String.Format function to format a string into Decimal number format. How to format a Double in C# using String.Format For example, if you have to format a number up to two decimal places, use a pattern that specifies two zeros after point such as 0.00.... » read more

C# FileStream: open, read, write files in C#

In .NET Framework, the System.IO namespace contains classes and functions that are used for file-related functionality, such as creating, reading, writing, updating, or closing a file. In this article, we will look at the FileStream class and its importance in WinForms applications. Using FileStream Class in C# for file operations It is a recommended programming... » read more

Introduction to File attributes in C#

In this article, we will learn about file attributes in C#, how to set or get file attributes, and the FileAttributes Enum. FileAttributes Enum The file Attributes is an Enum that contains attributes fields that can be set or get when dealing with files or directories. Here are the lists of the fields of the FileAttributes... » read more

How to get time information of a file in C#

In this article, you will learn how to get time-related information of a file. There are two different methods to achieve this. You can either use static methods of File class or call methods of FileIno class after initiating it. Get time information of a file in C# using the File Class Let’s say you... » read more

Catch Unhandled Exceptions in WinForms Apps using C#

In this article, we will learn how to handle an unhandled exception being thrown in C#. An exception is an error that occurs at runtime, and an unhandled exception occurs when the application fails to handle the exception being thrown properly. Unhandled Exceptions in C# Applications The UnhandledException Event notifies an app whenever an unhandled... » read more

Load Text from File to String variable using C#

In this example, we will show you how to load a text file into a string variable in C#. For this purpose, we are going to use the StreamReader.ReadToEnd method to read from a text file on your local machine, or on a network folder. First, you will need to add the namespace below: using... » read more

Convert HTML table to JSON string in ASP.NET using C# and JavaScript

In this tutorial, I will explain to you how to convert an HTML table to a JSON string using C#. Convert HTML table to JSON in ASP.NET To get started, insert a static table and an ASP button as shown in the code below: <table class="nav-justified" border="1" id="tblEmployees"> <tr> <td><strong>Name</strong></td> <td><strong>Department</strong></td> </tr> <tr> <td>Jeff</td> <td>IT</td>... » read more

Introduction to Delegate in C#

Delegate is a reference type variable that can hold the reference of a function. It is like a pointer to a method, in C++ or C. Delegates are implicitly driven from the System.Delegate Class. We can say that it is an object, or reference type variable, which refers to a function, and can hold the... » read more

Create HTML Table dynamically in ASP.NET using C#

In this example, we are going to show you how to create an HTML table dynamically in ASP.NET using C#. Create an HTML dynamically in ASP.NET For many reasons, a developer might need to create a table dynamically. For example, looping through a DataTable in order to print out the rows on the web page... » read more

Create a Global Error Handler in C# for a Console Application

It’s easy enough to handle the exception using try/catch blocks. Some application takes benefit of the global exception handler. For Example, if you want to control standard exceptions, you can use your custom global exception handler to handle exceptions such as divided by zero, ArrayOutOfBoundException etc. These exceptions may occur when an unhandled exception is found.... » read more

Check if a number is prime using C#

Below is a program in C# that check whether a number is prime or not. The program inputs a value form the user and print whether it is prime or not. What is a prime number? Every number is a prime number or a composite number except 0 and 1. A prime number is a... » read more

How to Swap Two Numbers using C#

Swapping of two numbers refers to the exchanging values of two variables. Here are two methods which we can use for swapping two integers Swap using a temporary variable Swap without using a temporary variable Swap two integer using a temporary variable This swap operation is performed by using a third variable (a temporary variable). Here... » read more

Check if a Year is Leap Year or Not using C#

There are 356 days in a common year while a leap year has 366 days. February 29 is a date Which occurs once after every four years and is called leap day. February 29 is included to the leap year calendar as leap day because the Earth does not complete its orbit around the sun... » read more