C# Program Structure And Basic Syntax With Examples

This Tutorial Explains C# Program Structure And Basic Syntax. You will Learn the Usage and Declaration of Various Components of a C# Program:

A Complete Introduction to C# and Visual Studio was given in our last tutorial. We created a simple hello world program and discussed the different components of that program.

In this tutorial, we will take C# programming forward and will be discussing the basics syntax and program structure that constitute a proper C# program.

=> Read Through The C# Guide For Beginners Here

C# program structure and basic syntax

Let’s again have a look at the previous program that we wrote. We wrote this program to print out a simple text in the console i.e. “Hello World”. But if you look closely at the program there are several other components in the program that make it.

Keep this program in mind as we are going to take this program as a reference for the upcoming topics.

A typical c# program

If we look at the above program we will find that the above program consists of several different components.

C# Program Structure

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

  1. Namespace
  2. Class
  3. The main method
  4. Methods inside the class
  5. Class definition or class attributes
  6. Statements
  7. Comments

Few things need to be kept in mind while writing a C# program. C# programs are case sensitive, which means “string” is different from “String”. All the statements written in the program must be concluded with a semicolon i.e. “;”. A semicolon tells the program that the current line of the statement has ended.

As discussed earlier, the execution of the C# program starts from the Main method, hence the program should have the Main method as its starting point.

Before we begin with an explanation of the C# components lets recall a few points:

  • A program needs to have a reference to the .Net framework that has been used in the program. For our current program to reference.Net, we use “using”. For Example “using System” in our above program.
  • After defining the references we declare a namespace. Syntax Example- namespace ConsoleApplication.
  • Then we declared a class using a class keyword. Syntax Example- class Program.
  • Inside the class, we have a Main method. This is the starting point of the execution.
  • Inside the Main method, we have defined “Console” which is a class in the .Net framework. The “WriteLine()” is a method that is used to display a message in a console.
  • Similar to “WriteLine()”, “ReadLine()” is also a method that reads user input.

In the above program, we wrote a text in the console application and discussed various parts of the program. Here, we will discuss various parts that were used in conjuncture with each other to make-up a valid syntax in detail. We will discuss components, their definition and ways in which we have to declare them in the program.

Using Keyword

The using keyword is used to handle any object that is being used to implement the namespace. It is used to import a namespace. As a namespace is a collection of classes and each class has its own functionality, so using the keyword provides us with the facility to implement different functionality from the namespace that has been imported.

Namespace

The namespace is a collection of related classes and objects. It is designed to offer a way to keep a different set of objects segregated from each other. This allows the programmers to declare one class inside a namespace and another class inside another namespace without causing a conflict.

Let’s say we are creating two namespaces namely “fruits” and “colors”. Both these namespaces can have a class called “Orange” without creating any conflict with each other. A namespace is defined by writing your namespace name preceded by keyword namespace.

For Example:

namespace ConsoleApplication

Class

Defining a class is similar to defining a blueprint of a data type. Class doesn’t actually define the data but they arrange the data as a meaningful entity.

For Example, if we have a class called “Fruit”, then we can specify different related objects inside the class such as mango, apple, grapes, etc. Thus, the class fruit will contain different fruit objects with different properties such as color, taste, smell, edible, etc. Each of these objects and property will be a part of the class.

Similar to the above example, the object-oriented programming language such as C# also defines different properties inside i.e. fields, methods, conditions, events, etc. The objects inside the class contain a definition of the operations that can be performed.

Objects are basically an instance of the class and the methods or the variables that are the members of the class. Thus, in short, the class allows you to create your own custom objects by grouping other methods, events and object types.

A class is defined by putting the keyword “class” before the name of the class and the class body should be defined by a pair of curly braces.

class Program
{
// class body
}

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.

For Example:

public class Program // access modifier set as public
{
}

Method

A method is a group of statements that execute together to perform a certain task. Every C# program will contain at least one class with at least one Main method.

A method contains the following definition. It starts with an access modifier declaration, then a return type and later we define the method name and inside the round bracket, we have the parameters defined.

{Access Modifiers} {return type definition} Method Name({parameter})
public static void Main(String[] args){
}

After the method has been defined, a set of commands or statements can be written within the curly braces. Arguments can be passed within the round bracket just after the method name.

These arguments are useful while calling a method with certain parameters. In the above example, we have passed only one parameter that is “args” with the argument type as an array of string.

Constructor

Constructors are the special methods in a class that is automatically invoked whenever an instance of the particular class is created. The main advantage of the constructor is that it can initialize the private fields of a class. A class can have a number of constructors and it doesn’t need to have any return type.

Only one static constructor is allowed inside a class and that cannot be parameterized. Constructors are declared by using any access modifier and then by using the class name in which it is created.

For Example, a constructor definition will look something like this.

class Program
{
public class(){ //This is a constructor
}
}

Data Types

Data types are used in almost all the programming languages. A data type tells the compiler what kind of value a variable will hold. C# has several built-in data types such as String, Integer, Float, Boolean, etc. Each data type has its own set of definitions for the values it can hold.

A data type is used to declare a variable by the preceding variable with the data type keyword.

class Program
{
static void Main(string[] args)
    {
string stringValue = "Hello";
int integerValue = 10;
float floatValue = 13.2f;
bool booleanValue = false;
    }
}

We will discuss more on data types in our upcoming tutorials.

Curly Braces

We saw the extensive usage of curly braces “{}” in our previous programs and declarations which is why it is referred to as one of the most important parts of any program. It defines the beginning and the end of any logical statement in a program block.

The curly braces are not only restricted to the C# language, but they are also found in several other languages like Java, C++, etc. You can clearly see our first “Hello World” code that they are used to wrap multiple lines of code together. It clearly marks the starting and the endpoint of the logical statement for the compiler.

Any logical entity like Namespace, Class, Method, Conditional statement, Loops, etc. should be enclosed inside braces to clearly mark the start and end of the statement for the compiler.

Conclusion

In this tutorial, we learned the usage and declaration of various components of the C# program. We gathered some high-level understanding of the concepts. We learned how to declare various components and usage of various components like classes and methods.

We also discussed the importance of using curly braces and learned a few things about constructor. In our upcoming tutorials, we will discuss all these components that make up your program in detail and see how to use them to implement different scenarios.

Take Away

  • A namespace is a collection of one or more classes and is designed to segregate different sets of objects/classes.
  • Class is a collection of methods, statements, events, properties, etc. of similar type.
  • Access modifiers are used to define the accessibility level of a class or method.
  • Constructors are special methods with the same name as the class and are invoked as soon as an instance of the class is created.

=> Explore The Entire Series Of C# Training Tutorials Here