GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C# - Program Structure
Selected Reading
C# - Program StructureBefore we study basic building blocks of the C# programming language, let us look at a bare minimum C# program structure so that we can take it as a reference in upcoming chapters. Basic Structure of a C# ProgramA C# program follows a simple structure with essential components. Heres what a basic program looks like:
Creating Hello World ProgramLet us look at a simple code that prints the words "Hello World" − using System;
namespace HelloWorldApplication {
class HelloWorld {
static void Main(string[] args) {
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
When this code is compiled and executed, it produces the following result − Hello World Let us look at the various parts of the given program −
Key Components of a C# Program1. Namespace DeclarationNamespaces help organize large programs by grouping related classes together. namespace MyApplication
{
class Program
{
static void Main()
{
Console.WriteLine("Hello from MyApplication!");
}
}
}
When the above code is compiled and executed, it produces the following result − Hello from MyApplication! 2. Using Directives (using)The using System; // Enables Console.WriteLine() function
class Example
{
static void Main()
{
Console.WriteLine("Using directive example.");
}
}
When the above code is compiled and executed, it produces the following result − Using directive example. 3. Class DeclarationC# is an object-oriented language, and every program must have at least one class. class Car
{
string model = "Tesla";
static void Main()
{
Car myCar = new Car();
Console.WriteLine("Car Model: " + myCar.model);
}
}
When the above code is compiled and executed, it produces the following result − Car Model: Tesla 4. The Main() MethodThe using System;
class Start
{
static void Main()
{
Console.WriteLine("This is the main entry point of the program.");
}
}
When the above code is compiled and executed, it produces the following result − This is the main entry point of the program. 5. Statements & ExpressionsEvery instruction inside the class StatementsExample
{
static void Main()
{
int a = 5, b = 10;
int sum = a + b;
Console.WriteLine("Sum: " + sum);
}
}
When the above code is compiled and executed, it produces the following result − Sum: 15 6. Access ModifiersAccess modifiers define the visibility of classes and methods. class Example
{
private int secretNumber = 42; // Private: Only accessible within this class
public void Display()
{
Console.WriteLine("Access Modifier Example: " + secretNumber);
}
}
class Program
{
static void Main()
{
Example obj = new Example();
obj.Display();
}
}
When the above code is compiled and executed, it produces the following result − Access Modifier Example: 42 Organizing a C# ProgramA well-structured program is easier to read, runs better, and is simpler to maintain.
Example: Well-Structured C# ProgramHere's a real-world example demonstrating a structured C# program with multiple components: using System;
namespace School
{
class Student
{
public string Name;
public int Age;
public void DisplayStudentInfo()
{
Console.WriteLine("Student Name: " + Name);
Console.WriteLine("Student Age: " + Age);
}
}
class Program
{
static void Main()
{
Student student1 = new Student();
student1.Name = "Alice";
student1.Age = 14;
student1.DisplayStudentInfo();
}
}
}
When the above code is compiled and executed, it produces the following result − Student Name: Alice Student Age: 14 It is worth to note the following points −
Compiling and Executing the ProgramIf you are using Visual Studio.Net for compiling and executing C# programs, take the following steps −
Compiling and Executing the Program Using Command-lineYou can compile a C# program by using the command-line instead of the Visual Studio IDE −
Advertisements
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |