C# Program Structure Assignment Help
PROGRAM STRUCTURE
C# Program Structure Assignment Help By Online Tutoring and Guided Sessions from assignmenthippo.com
The Structure of C# program includes
- Namespace declaration
- A className
- classNamemethods
- classNameattributes
- A Main method
- Statements and Expressions
- Comments
e.g. Example 1
using System;
namespace FirstExample {
classNameProgram
{
static void Main(string[] args)
{'/* my first example in C# */'}
{
Console.WriteLine({'"Simple Example"'});
Console.ReadKey();
}
} }
Output:
Simple Example
The structure of Example 1 is explained as follows
- using System is used to include the namespace System in the program.
- namespace declaration represents bunch of classes
- classNamedeclaration has the data and method definitions in a program
- A classNamecontains multiple methods. Methods define the role of the className. Program classNamehas only one method Main.
- Main()method is the entry access point for all C# programs.
- The line /*...*/ is comment line ignored by the compiler in the program
- WriteLine()is a method of Console classNamewhich is defined in the System namespace
- WriteLine("Simple Example") prints Simple Example as an output on the screen
- ReadKey() makes the program to wait for a key press and it prevents the screen from an abnormal exit .
- It is worth to note the following points −
To create a C# program there are 4 ways
- Simple Example
- Using System
- Using public modifier
- Using namespace
C# Program using Simple Example
classNameProgram
{
static void Main(string[] args)
{
System.Console.WriteLine("Simple Example");
}
}
Output:
Simple Example
C# Program using Using System
Here no need to give System namespace for accessing any classNameof this namespace. Console classNameis used without specifying System.Console.
using System;
classNameProgram
{
static void Main(string[] args)
{
Console.WriteLine("Program Using System");
}
}
Output:
Program Using System
C# Program using public modifier
Here do specify public modifier prior to classNameand Main() method. Now, it could be accessed from outside the className.
using System;
publicclassNameProgram
{
public static void Main(string[] args)
{
Console.WriteLine(“Program using public modifier");
}
}
Output:
Program using public modifier
C# Program using namespace
Namespace is used to group related classes together and also categorize classes for ease maintenance
using System;
namespace Example
{
public classNameProgram
{
public static void Main(string[] args)
{
Console.WriteLine("Program using namespace");
}
}
}
Output:
Program using namespace
Compiling and Running a C# Program
Command-line compilation of a C# program
- Open a text editor and type the code.
- Save the file as cs
- Open the command prompt and move to the directory where in the file is saved
- Type csc program.csand press enter to compile
- If there is no error, the command prompt generates helloworld .exeexecutable file.
- Type programto execute the program.
- The output gets printed on the screen.
Visual Studio.Net for compiling a C# program
- Start Visual Studio
- Choose File -> New -> Project from the menu option
- Choose Visual C# from available templates, and then choose Windows.
- Choose Console Application.
- Give a name for your project and click OK button.
- This generates a new project in the Solution Explorer window.
- Type the code in the Code Editor.
- To execute the project, Click the Run button or press F5 key
- A Command Prompt window appears that displays the output