In this article I am going to discuss about C# basic structure and syntax of csharp programming language. In my previous article I have discussed the C# introduction that includes what is csharp programming language , its feature and and different types of frameworks of C# programming language. This article is all about the C# basic structure and syntax that is used to make writing good program for our application.

C# Basic Structure

For writing C# Program you have to install visual studio. Visual Studio is not just for writing csharp(c#) program only but also used for writing robust and optimized code for our backend. You can download visual studio from this link.

using System;

namespace HelloWorld   //HelloWorld is class name
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("WebTutorialStack.com");    
    }
  }
}

Above is the simple structure of C# program. In C# every program starts with creating class. The each and every line of above program is explained below :

1. using System : This means that we can use classes from system namespaces. By default if we create class then this namespace is used.

2. namespace : Namespace is used to wrap your code in one container so you can use this class and its properties anywhere in your application by using its namespace.

3. curly braces ( {} ) : Curly braces are used to mark the begining and ending of code.

4. class : Class is the container for data members and member functions which brings functionality to our program. In C# Every line of code must be inside class otherwise it will throw error.

5. main method : In above program ‘static void Main(string[] args)’ is the main method. Only the code inside the main method will be executed. So in order to execute your program you must write your code inside the main method curly braces.

6. Console.WriteLine : Console.WriteLine is used to print text to terminal. Console is class of system namespace and Console.WriteLine is the method of this class.

Points to Remember

  1. Every C# statement ends with a semicolon (;).
  2. C# is case sensitive language which means myProgram and MyProgram are two different names.
  3. Unlike java it is not necessary that class name and file name should be same, you can give different class name from the file name.
  4. By mistake if you not using the system namespace then for printing text you have to write like System.Console.WriteLine(‘WebTutorialStack’);.
  5. Black spaces are ignored in C# program so you can use this to make your code readable.
  6. For writing comment in your code you can do like given below. After making it to comment this line of code will not executed when we run the program.
   //System.Console.WriteLine('Make your career with full stack web development');

Methods of Console Class in C#

There are so many methods of Console class are there in c#. In C# basic structure we have discussed one of the method Console.WriteLine which is used to print the text. But instead of this there are many more methods are there which is used for different purposes. As there are so many methods of Console Class so we only discuss the important methods of Console class which is used mostly.

1. Console.WriteLine() : As we already discussed this method is used to print the text. But this method not only print the text but it will also move the cursor to the next line so when we print another line the line will print in the next line.

Console.WriteLine("This is ");
Console.WriteLine("WebTutorialStack");

//Output:
This is 
WebTutorialStack.

2. Console.Write() : This method is also used to print the text but this method will not move the cursor to the next line so when we print the another line the line will print with the same line. Like

Console.Write("This is ");
Console.Write("WebTutorialStack");

//Output:
This is WebTutorialStack.

3. Read() : This method is used to read the single character from keyboard and returns the ASCII Value.

4. ReadLine() : This method is used to read the string from keyboard and returns the entered value. But this will only return the string so in order to read the number datatype we have to first read this as string then manually typecast to int. For example :

Console.WriteLine("What is your name");
Console.ReadLine();  //here we are reading the whole string.

//But if want to read number then we have do read like that
Console.WriteLine("What is your age");
Convert.ToInt32(Console.ReadLine());

5. ReadKey() : This method reads a single character from the keyboard and returns that character information like what key has been entered, and what its corresponding ASCII value is.

Conclusion

In above C# tutorial article we have discussed the C# basic structure and methods used in console class. This is the basic structure and keywords that is required to make your first ‘Hello World’ Program. If you have made this program then congratulations you have write the first C# Program. In my next article I am going to discuss the various data types of C# programming language.