Topic : Our First Program in C# | C# Programming Structure:

The Structure of C# Program:

In Our Previous Tutorial We discuss, how to Create our First Project in Visual Studio Environment. (So if you guys didn't read the previous tutorial, then it's recommended to read that tutorial first.) In This tutorial, we will Create our First Program, and Will discuss the Basic structure of C# program.

But Before Getting Start, Let Look at a the Program structure so that we can take it as a reference in Our Upcoming Tutorials.





Now Let's write a simple code to Know exactly how C# Programming structure look likes.



using System;


namespace firstProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Csharp Tutorials.");
            Console.ReadKey();
        }
    }
}


Above, There is an simple C# Program, To Print a simple message in console Windows. The program is really simple but we need to discuss alot About this Program, Because it's the first program in our Tutorials. So We'll discuss the Program Line by Line.



  • Using System:

    The Using Keyword is used to Include the "System" Namespace in the Program. A Program Generally has Multiple using Statements. A System Contains Commonly-used types. These are Separate from the Language-level aliases in the C# language. System can be referenced with Different Syntax forms. it is Usually specified with a using System Directive.



    Namespace

    The Namespaces are C# Program elements designed to Help us to Organize our Program. They also provide Assistance in Avoiding name clashes between two sets of code. Namespace is also the collection of Classes. our "FirstProject" Namespace contains the Class "Program" (Read more...)



    Class Declaration:

    IN Next Line, we declare our Class inside the Namespace. The Class is like a Blueprint. It Defined the Data and Behavior of Type. The Class "Program" Contains the Data and Methods that our Program uses. Generally, Classes contain Multiple Methods. Methods define the Behavior of the Class. However, the "Program" Class has only one Method "Main" (Read more...)



    Main Function

    In Next Line we declare our Main Function. The main Method is the Entry point of a Program. It is where the Program control starts and ends. The Main Function Must be Static Static mean, Relating to Type itself, rather than an instance of the type. Not be Public. Main function can either have a void or Int return type. Our Class won't return any thing so we used "void" . The Main Method can be Declared with or without a "String[]" Parameter that contains command-line arguments.



    Console.writeline();

    In Main method, We write an Statement "console.writeline();" The "Writeline" is the Method of a Console class defined in the System Namespace. This statement causes the Message "Welcome to CSharp Tutorials" to be Displayed on the Screen.



    Consolel.Readkey();

    The last line Of our Program is "console.Readkey(); " is for the VS.NET users. This makes the Program wait for a key press and it Prevents the Screen from running and Closing Quickly When the Program is Launched from Visual Studio.





Pay Attention to Detail:

In Programming the Details Matter. This is a Very Important skill. A Good Programmer Always analyzes the Problem Statement very Carefully and in Detail. We should pay attention to all the aspects of the Problem. We can't be Vague. We Can't Describe our Program 3/4th of the way, then say, " You Know what i mean?". and have the compiler figure out the rest.

Furthermore We should pay attention to the Calculations involved in the program, its Flow, and most Importantly, the logic of the Program, Sometimes, a Grammatically correct sentence does not make any sense. For Example: Here is a verse from Poem:


Twas brillig, and the slithy toves Did gyre and gamble in the wabe


The Grammar is correct but there is no Meaning, Similarly, The Sentence, "MR ABC Sleeps thirty Hours every day" Is grammatically correct but it an Illogical.

So it may happen that a Program is Grammatically correct, it Compile and Run but Procedures Incorrect or Absurd results and does not solve the Problem. It is very Important to pay attention to The Logic of the Program.



  • Compiling And Executing the Program

    In our Previous Lecture We already Discuss that how we can create a Project and how to compile it. Let's Discuss again here.
    » Start Visual Studio.
    » On the Top menu bar, Choose File > New > Project
    » Choose Visual C# From Templates, and Then Choose Windows.
    » Choose Console Application.
    » Specify a Name for the Project and Browse the Location where you want to save it. and click ok
    » Your Project has been created in Solution Explorer.
    » Write the code in the code Editor.
    » And Click the Run Button or Press F5 To execute the Project.

How to compile C# program : infobrother













I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement