Before studying the entire C# programming language, let us first know a simple program structure where every component will be explained in detail. To begin learning this programming language, and before proceeding with other programming concepts, you need to know some minimal structure and syntax. In this chapter, you will learn about the specific and least structure of C# programming.



The lists of elements that are commonly used in a C# program are:
  • Library invoked by the keyword "using".
  • Declaring namespace using the "namespace" keyword.
  • Declaring a class using the keyword "class".
  • Class members and attributes.
  • The Main() method.
  • Other statements and expressions.
  • Writing comments for user understanding and non-executable statements.

Let's begin with a simple C# program.

C# Program which Outputs a Line of Text

using System;
namespace printHelloCsharp
 {
   class HelloCsharp {
      static void Main(string[] args) 
      {
         /* Print some string in C# */

         Console.WriteLine("Hello C#.");
         Console.ReadKey();
      }
   }
}

Program Output:

Hello C#.

Let's look into various parts of the above C# program.

/* Comments */ /* Print, some string in C# */, is the comment statement that does not get executed by the compiler and is used for code readers or programmers' understanding.
using Here, using keyword is employed for fetching all the associated methods that are within the System namespace. Any C# program can have multiple using statements.
namespace This next statement is used for declaring a namespace, which is a collection of classes under a single unit; here, "printHelloCsharp".
Class Next, you have declared a class "HelloCsharp" using the keyword class, which is used for preserving the concept of encapsulation.
main() Inside a class, you can have multiple methods and declaring variable names. The static/void is a return value, which will be explained in a while.
Console.WriteLine() Console.WriteLine() is a predefined method used for displaying any string or variable's value in a C# program.
Console.ReadKey() Console.ReadKey() is another predefined method used to make the program wait for any key-press.


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram