C# Program structure and Basic Syntax

This article Explains C# Program Structure And Basic Syntax. We will Learn the Declaration and usages of Various Components of a C# Program.

C# Program structure and Basic Syntax

C# is a popular programming language developed by Microsoft for building Windows applications, web applications, games, and more.

C# Program Structure

A typical C# Program consists of different parts as shown below:

  • Namespace
  • Class
  • The main method
  • Methods inside the class
  • Class attributes or definition
  • Statements
  • Comments

Here is an overview of the basic syntax and structure of a C# program:

1. C# is case-sensitive, meaning that uppercase and lowercase letters are considered different. For example, “Hello” and “hello” are two different identifiers.

2. C# programs begin with the Main method, which is the entry point of the program. The Main method has the following syntax:

static void Main(string[] args)
{
    // Code to be executed
}

3. C# uses semicolons (;) to mark the end of statements.

4. C# uses curly braces ({}) to define code blocks. A code block is a group of statements that are executed together. For example:

if (condition)
{
    // Code to be executed if the condition is true
}
else
{
    // Code to be executed if the condition is false
}

5. C# uses double slashes (//) for single-line comments, and /* and */ for multi-line comments. Comments are ignored by the compiler and are used to add notes or explanations to the code.

6. Namespace: A namespace is a grouping mechanism that organises related classes and objects. Its purpose is to prevent conflicts between different sets of objects by segregating them from each other. With namespaces, programmers can define classes in one namespace without interfering with classes in another namespace.

C# programs are typically organised into namespaces, which are containers for related classes and other type. The namespace declaration comes at the top of the file and looks like this:

 namespace MyNamespace
{
    // Classes and other types go here
}

7. Class: In C#, a class is a blueprint for creating objects. Classes can contain fields, properties, methods, and other members that define the behaviour and properties of the objects they represent. Here is an example of a class:

public class MyClass
{
    private int _myField;

    public void MyMethod()
    {
        // Code goes here
    }

    public int MyProperty { get; set; }
}

8. Statements and expressions: In C#, statements are instructions that perform some action, such as assigning a value to a variable or calling a method. Expressions, on the other hand, are combinations of values, operators, and method calls that evaluate to a single value. Here are some examples of statements and expressions:

int x = 10; // statement
int y = x + 5; // expression
Console.WriteLine("Hello, world!"); // statement

9. Control flow statements: C# includes a variety of control flow statements that allow you to control the flow of execution in your program. These include if/else statements, switch statements, loops, and more. Here are some examples:

if (x > 10)
{
    // Code goes here
}
else
{
    // Code goes here
}

switch (x)
{
    case 1:
        // Code goes here
        break;
    case 2:
        // Code goes here
        break;
    default:
        // Code goes here
        break;
}

while (x > 0)
{
    // Code goes here
    x--;
}

10. Variables: Variables are used to store data in a C# program. C# supports several types of variables, including integers, floating-point numbers, strings, and boolean. Variables are declared using the var keyword.

int myInt = 42;
float myFloat = 3.14f;
string myString = "Hello, world!";
bool myBoolean = true;

11. Operators: Operators are used to perform operations on variables in a C# program. C# supports several types of operators, including arithmetic operators (+, -, *, /), comparison operators (==, !=, <, >), and logical operators (&&, ||).

int x = 10;
int y = 20;
int z = x + y; // z is now 30
bool b = (x < y) && (y < z); // b is true

Access Modifiers:

Access modifiers define the accessibility of an object and its components. All the C# components have their own access level that can be controlled by defining the scope of accessibility for the member objects inside the class by using access modifiers.

To define the accessibility level of the object we have to declare it by using one of the keywords provided by C# language i.e. Public, Private, Protected and Internal. Access modifier is declared by using either of the keywords mentioned above before the class or a method.

  • Public: Members with the public access modifier are accessible from anywhere in the application, both inside and outside of the class or struct. Example:
public class ExampleClass
{
    public int publicField = 10;
    public void PublicMethod()
    {
        // Method code here
    }
}
  • Private: Members with the private access modifier are only accessible within the same class or struct.

Example:

public class ExampleClass
{
    private int privateField = 10;
    private void PrivateMethod()
    {
        // Method code here
    }
}
  • Protected: Members with the protected access modifier are accessible within the same class or struct, and any derived classes.

Example:

public class BaseClass
{
    protected int protectedField = 10;
    protected void ProtectedMethod()
    {
        // Method code here
    }
}

public class DerivedClass : BaseClass
{
    public void SomeMethod()
    {
        // DerivedClass can access protectedField and ProtectedMethod from BaseClass
    }
}
  • Internal: Members with the internal access modifier are accessible within the same assembly (i.e., project). Example:
internal class ExampleClass
{
    internal int internalField = 10;
    internal void InternalMethod()
    {
        // Method code here
    }
}
  • Protected internal: Members with the protected internal access modifier are accessible within the same assembly and any derived classes, regardless of whether they are in the same assembly or not.

Example:

public class BaseClass
{
    protected internal int protectedInternalField = 10;
    protected internal void ProtectedInternalMethod()
    {
        // Method code here
    }
}

public class DerivedClass : BaseClass
{
    public void SomeMethod()
    {
        // DerivedClass can access protectedInternalField and ProtectedInternalMethod from BaseClass
    }
}

Leave a Comment

RSS
Follow by Email
YouTube
YouTube
LinkedIn
Share