To write your first simple C# program, please open the Notepad application(comes preinstalled in Windows Operating System)
and
type in your first C# program, as explained in this article.
You may even use an Integrated Development Environment(IDE) application like Microsoft Visual Studio and NetBeans
to create your first C# program but for a beginner,
we would recommend using Notepad, because you learn a lot when you work to find your own mistakes to fix them,
as compared to an IDE finding them for you.
Important points about a class in C# -
Every C# program starts with a class.
A C++ class may have zero or more data members, which are also known as instance variables
A class describes its behaviour through its methods,
where a method is a place where the logic of a class is stored.
A C# class may have zero or more methods.
C# naming conventions -
Technically, every class name in C# should start with a capital alphabet
and a multiple word class name follows a camelCase style i.e. with no space
or underscore in between multi-word class name.
Every method name defined in a C# class should also start with a capital alphabet
and a multiple word method name follows a camelCase style i.e. with no space
or underscore in between multi-word method name.
Note :Not following these naming conventions while naming an identifier won't raise any error i.e.
C# compiler doesn't throw a compile error, if a class name starts with a lowercase letter,
or if a multiple word method name is
separated by b underscore (_), but, it is good to follow the naming conventions of defined by the language in which you are coding as
it also improves the readability.
C# is a case-sensitive language
C# is a case-sensitive language, hence a class containing two variables with one named a
and the other named A, are treated differently and methods named message() and Message() are treated as two separate methods.
Creating our first C# program
We are creating a C# program which will be saved in a file named A.cs.
Why .cs extension for C# program?
Every C# program is saved with a .cs extension because cs stands for C Sharp.
A.cs
//C# A simple program
using System;
//Creating a class named A
class A
{
//Defining the method named Main() of class A
public static void Main(String[] ar)
{
//Using the Console.WriteLine statement to print a message to the Console
Console.WriteLine("Hello from C#");
}
}
Advertisement
Understanding your first C# program
Let's see what we actually did in our first C# program by dividing it into a few segments -
using System;
Every C# program that requires to perform input and output operations,
must use an already defined System namespace by using keyword right at its beginning i.e. before any class is defined.
Doing this, tells the compiler to look in the System namespace
(namespace can be understood as a container for a set of classes), in order to use the methods or variables defined in it to perform input-output operations.
class A
Every C# program is contained within a class. Hence, we have created a class named, A, and it is defined
within a pair of curly braces { }.
According to the C# convention,
every class name in C# should start with a capital alphabet, hence, we have named our class A.
Any new class created in C# is automatically given the publicaccess modifier,
which makes this class accessible to all other classes in C#.
Hence, our class A is accessible to all other C# classes.
Note: Giving any other access modifier
like private, protected, etc to a class in C# is illegal and is not allowed.
Unlike Java, in C#, the name of the program file with .cs extension
need not to be matched with the name of public class defined in it.
Note : Unlike Java, In C#, we can define any number of public classes in a single .cs program file.
public static void Main(String[] ar)
In our class, we have defined a Main() method,
because every C# program begins its execution with a call to the Main() method of the class,
which is also known as the entry point of the program to execute.
The Main() method is always defined with a public access modifier,
which makes it accessible from the outside of its class, to execute it.
Unlike Java, the Main() method in C# starts with a capital letter, by following its method naming conventions.
The Main() method is always defined with static keyword,
which means that this method can be directly accessed/called, without having to instantiate or create an object of class in which it is defined, i.e. class A.
The return type of Main method is always void,
which means that this method will not return any value after it finishes its execution.
The Main() method has one parameter named ar, which is an array of class type String,
it means we can pass one or more objects of String class to the main method.
If you don't understand much of what is explained so far in this program, don't worry, you
will learn more about array,
static keyword and
String class in the upcoming articles.
Console.WriteLine("Hello from C#");
This line simple outputs the string Hello from C# on the command prompt, where :
Console.WriteLine() is method which
writes the specified data followed by the current line terminator or a new line character to the standard output stream i.e. console.
The Console.WriteLine is a predefined method, which is defined within the System namespace
and that's why we had specified the System namespace at the beginning of the program.
Advertisement
How to compile a C# program?
In order to compile our C# program, we first need to open the Developer Command Prompt for Visual Studio application,
which was installed when we installed Microsoft Visual Studio.
Next, on the window of Developer Command Prompt for Visual Studio, we need to call the
C# compiler with a command - csc, followed by a space, followed by the
the name of our file(with its extension) that contains the C# class.
We have saved our C# program in a file A.cs in the E: Drive location. Hence, in order to compile our A.cs
file from Developer Command Prompt for Visual Studio, first we need to reach the
location where this C# file is saved and type in the command -
E:\csc A.cs
This will create a file, A.exe. This .exe file is a compiled form of our C# program,
which is going to be required to execute our C# program.
Note :
The compile command csc stands for C Sharp Compiler.
How to run a C# program?
E:\A
Hello from C#
In order to run our program,
we just need to type in the name of our C# program file that contains the Main() method i.e. A.
And, at the end of this command, we press Enter to get the output of our program.
And, this concludes a detailed description of the concepts of a simple C# program.