Introduction to File Handling Methods in C#

The File class belongs to System.IO namespace. It provides static methods to create, open, copy, delete or move the file. The parent class of File is the Stream class. A stream is an abstract class. When you open a file for reading or writing purposes, it becomes a stream. Input and Output are two basic... » read more

How to Log out of session in MVC

Logging out is an important step when it comes to security. Simply closing the browser may not clear the data you were accessing. Below is a function that you use from inside a controller to log out the right way. Log out action in MVC public ActionResult LogOut() { FormsAuthentication.SignOut(); Session.Abandon(); return RedirectToAction("index", "home"); } Happy Coding!!! Related Articles Create... » read more

Create a newline in rich text box in C#

There are two ways to create new lin in RichtTextBox control in C#, by  using: RichTextBox.Text property RichtTextBox.AppendText method RichTextBox.Text property myRichTextBox.Text += Environment.NewLine + "New line goes here"; RichtTextBox.AppendText method myRichTextBox.AppendText(Environment.NewLine + "New line goes here");   Related Articles Export Data from DataTable to PDF in C# using iTextSharp Create Your First C# Program Create... » read more

Introduction to Stack in C#

When it comes to data structures, one of the frequently used data structure is Stack. It represents Last in First out (LIFO) concept. Just to give a brief idea about the LIFO, it is a concept that used to remove last elements that added to stack first. Meanwhile, it is not possible to remove elements... » read more

Overview of dependency injection in C#

Dependency Injection (DI) could be a software system style pattern. It permits us to develop loosely-coupled code. The intent of Dependency Injection is to create code rectifiable. Dependency Injection helps to cut back the tight coupling among software system elements. (DI) reduces the hard-coded dependencies among your categories by injecting those dependencies at run time... » read more

Exception Handling in C#

An exception is a problem that arises during program execution. Exceptions are expected to happen within the code of an application for many reasons (known or unknown). Below is a tutorial on how exceptions work in C#. Exception Handling in C# Exception handling is a feature in C# to handle (exceptional circumstances) runtime errors in... » read more

Delete checked items from CheckedListBox in C#

Below is a sample code that will delete checked items from a ChekcedListBox in a windows form applications. Simply call the code from a button or any other controls of your choice. for (int i = clb.Items.Count - 1; i >= 0; i--) { // clb is the name of the CheckedListBox control if (clb.GetItemChecked(i)) { clb.Items.Remove(clb.Items[i]);... » read more

Replace hostname in URL using C#

Below is a function that replaces the hostname in a URL in C#. I take 2 arguments, the old and new host then returns the new host that can be pinged and browsed: public static string ReplaceHost(string originalHost, string newHost) { UriBuilder _builder = new UriBuilder(originalHost); _builder.Host = new Uri(newHost).Host; return _builder.ToString(); } Now call... » read more

How to Use Throw Expressions in C# 7.0

An unexpected runtime error arises in the code when an application or system constraint is being violated by the program, for example, when a program is dividing a number by zero, trying to connect to a non-existing database, or trying to open an XML file which is corrupted. We need a block of code to... » read more

Create Your First C# Program

Introduction C# (pronounced as C Sharp) is an object-oriented programming language that runs on the .NET Framework. It allows developers to create robust and secure applications. Let’s talk about the basic concepts of C#. Variables: Variables are used in the program to store data. When a variable is created, it reserves a memory location to... » read more

Get the IP Address of a local machine Using C#

The function below retrieves the IP address of a local machine using C#. This sample code uses the system.NET namespace to get the host name and then gets the IP address for that host name. C# private string GetIPAddress() { StringBuilder sb = new StringBuilder(); String strHostName = string.Empty; strHostName = Dns.GetHostName(); sb.Append("The Local Machine... » read more

Check for Even or Odd Number Using C#

This program checks whether a number entered in a textbox is an Even or Odd number. In math, a number who can be divided by 2 with a zero reminder Is an Even number. If a number is not divided by 2 then that number is indeed an odd number. This function is frankly easy... » read more

Create a Simple Thread Program Using C#

This sample code will teach you the basic of creating a thread in C#. The console app that we are about to create will a new ThreatStart delegate that will point to a method which will be executed by the thread. The delegate will be passed as a parameter whenever a new thread instance is... » read more