We will start with setting up the C# development environment, then we will move on to the IDE that we will be using for C# programming and then we will write our first program in C# and will understand it's structure.
C# Environment Setup
Step 1: Download Visual Studio from the official website of Microsoft: https://visualstudio.microsoft.com/downloads/
Step 2: Click on the downloaded installer's .exe file
Step 3: Run the installer file -> click Continue button -> select required product -> click Install button
Step 4: Select .NET desktop development -> click Install button
Step 5: Once installed successfully -> restart the machine.
Integrated Development Environment for C#
An Integrated Development Environment (IDE) is a GUI-based software application that provides facilities to computer programmers for software development. The most common features, such as source code editor, debugger, version control, and data structures browsing, help a developer quickly execute actions without switching to other applications. An IDE supports single or multiple languages.
Following tools are provided by Microsoft for C# programming. By using these tool you can develop from simple command-line applications to more complex applications.
-
Visual Studio (latest version 2019)
-
Visual C# Express
-
Visual Web Developer
Visual C# Express and Visual Web Developer are freely available tools so, you can download the latest version to use it. These two tools are Express edition and trimmed down versions of Visual Studio and have the same appearance. Now, let's understand the basic structure of a C# program.
To create a program, navigate to File menu tab to create new C# application (create a console-based application and run the following Hello, World! program).
First C# Program & its Structure
Basically, a C# program consists of namespace, class, class methods, class members, main method, instructions and expressions, and comments (single line or multiline). A C# program is saved in a file with extension .cs which stands for C sharp.
Filename: Program.cs
using System;
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.ReadKey();
}
}
}
Output:
Hello, World!
Explanation of the above program:
Line 1: using System;
: The using
keyword is used to include the System
namespace in the program (it is same as we are importing to use predefined functionalities). A program generally has multiple using
statements.
Line 2: In this line, we have declared a namespace
named as Studytonight followed by curly brackets. A namespace is a collection of classes. The Studytonight namespace contains the class Program. You can have any number of namespaces in a program.
Line 4: In this line, we have declared a class named as Program (default name given by IDE) followed by curly brackets. A class contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. You can have any number of classes and methods in a program but the class name must be the same as the filename where the main method is located. In our case, the Program is a class name where the main method is present. Hence, the filename is Program.cs. We have a separate chapter for the class where we will discuss in details.
Line 6: In this line, we have the Main
method followed by curly brackets, which is the starting point of execution for all C# programs. The Main
method states what the class does.
Line 8: In order to output something in C#, we can have to use the following method(s):
-
System.Console.WriteLine()
-
System.Console.Write()
WriteLine
and Write
are methods of the Console
class defined in the System
namespace.
Line 9: Console.ReadKey();
is for the VS.NET Users. This makes the program wait for a keypress and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.
Difference between WriteLine()
and Write()
method
The main difference between WriteLine()
and Write()
is that the WriteLine()
method prints a string and then moves to the start of the next line whereas the Write()
method only prints the string provided to it.
For example:
System.Console.WriteLine("Hello, ");
System.Console.WriteLine("World!");
Output:
Hello,
World!
If you put the Write
method instead of the WriteLine
method in the program above, then the output will be Hello, World! in the same line.