C# Program Structure

Before studying about the entire C# programming language, let us first know a simple program structure where every component will be explained in details. For starting a programming language, there are some minimum structural and syntactic architecture that one should know to proceed with other programming concepts. 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 which does not get executed by the compiler and is used for code readers or programmers' understanding.
usingHere, using keyword is employed for fetching all the associated methods that are within the System namespace. Any C# program can have multiple using statements.
namespaceNext statement is used for declaring a namespace which is a collection of classes under a single unit; here "printHelloCsharp".
ClassNext, 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.

 


Scroll Back to Top