Microsoft’s.NET strategy included the development of C#, a robust, multi-paradigm programming language. It is general-purpose, web-enabled, and object-oriented. When the.NET Framework was being developed, Anders Hejlsberg and his colleagues created C#. It is recognized as a standard by the International Organization for Standardization and the European Computer Manufacturers Association.
Structure Of C# Program with example
Several essential components make up a typical C# program, which arranges its code and specifies its execution flow:
Namespace Declaration: When utilizing various libraries, this helps to avoid name conflicts and organize code into logical groups by providing a declarative zone for identifiers such as types, functions, and variables. Using the using keyword is a common way to incorporate standard.NET namespaces like System.
Class: Being a really object-oriented language, C# requires that everything be contained within a class. Your program’s behaviour (methods) and data (fields) are defined by classes. At least one class is always present in a C# program.
Class Methods: A class’s actions or behaviours are defined by its methods. They are independent code blocks that carry out particular operations, accept parameters, and have the potential to produce a result.
The Main
Method: One of the classes in any C# executable program must have the Main()
function. All C# programs begin with this method, which specifies what the class performs when it is run.
- The
Main
method needs to be set tostatic
. Thestatic
keyword indicates that the method is global and does not need the creation of a class instance. - Usually, its return type is
void
, meaning function doesn’t return anything. It can return an integer as well. - Command-line arguments supplied to the program can be represented by a
string[] args
parameter, which can be optionally accepted by theMain
method. This array does not contain the name of the executable file, in contrast to C Language and C++.
Statements and Expressions: Declaring local variables, executing procedures, or allocating data are all done with a statement, which is a procedural building component. Typically, a semicolon (;) is used to end statements. An expression is a piece of code that evaluates to a single namespace, object, value, or method.
Comments: These serve to clarify specific code segments and are disregarded by the compiler. Single-line comments that start with // and multi-line comments that start with /* and end with */ are both supported in C#.
C# Basic Syntax Elements
Syntax structures in C# are quite similar to Java and closely resemble those in older high-level languages like C and C++. Crucial components consist of:
Keywords: These reserved terms have fixed meanings that are unchangeable and are predefined by the C# compiler. Every keyword needs to be put in lowercase letters. Using, if, else, for, while, class, static, void, public, private, int, string, bool, and so on are some examples.
Identifers: Program elements such as variables, types, and functions are identified by these names. Because C# is case-sensitive, Class and System are not the same thing.A console is not the same as a system.The console.
Literals: These are fixed values, like texts (like “Hello World”) or integer numbers (10000).
Operators: These are unique characters that manipulate one or more operands. Examples include logical operators (&&, ||), comparison operators (==,!=, <, >), assignment operators (=), and arithmetic operators (+, -, *, /).
Punctuators: Program structure is defined by these symbols, which include semicolons, curly braces, and parentheses.
“Hello World” Program Example and Explanation
Here is an example of a typical C# “Hello World” program:
using System; // Includes the System namespace
namespace HelloWorldApplication // Declares a namespace
{
class HelloWorld // Declares a class
{
public static void Main(string[] args) // Declares the Main method, the program's entry point
{
/* This is a multi-line comment.
It prints "Hello World" to the console. */
System.Console.WriteLine("Hello World"); // Statement to print text
// Console.ReadKey(); // Optional: keeps console window open in Visual Studio
}
}
}
Output
Hello World
Let’s deconstruct the code:
using System;
: With the help of this line, you can use types defined in the System namespace without having to provide their complete name (for example, Console.WriteLine rather than System.Console.WriteLine).
namespace HelloWorldApplication
: In order to organise the code, this establishes a namespace. Although it’s not required for tiny applications, it’s a good idea for bigger ones.
class HelloWorld
: The class HelloWorld is defined here. Every C# code must be contained in a class.
public static void Main(string[] args)
: The primary approach is this.
public
: An access modifier that makes the Main method available to everybody.Static
: declares Main as a global method that can be used without requiring the HelloWorld class to be created. As the point of entry for execution, the compiler saves its address.void
: The Main method returns nothing, according to this type modifier.Main
: (Note the capital ‘M’, unlike Java’s main) The entry point method’s traditional name.tring[] args
: Command-line arguments can be passed to the application as an array of strings with this optional parameter.
{}
: Code blocks for classes, methods, and namespaces are defined by curly brackets.
System.Console.WriteLine("Hello World");
: This sentence displays the string “Hello World” on the console using the WriteLine function from the Console class, which is a part of the System namespace.
Console.ReadKey();
: When running console apps from Visual Studio, this line is frequently used to halt the program and wait for a key press before the console window closes, revealing the output to the user.
Compilation and Execution
A few methods exist for compiling and running C# programs:
Using Visual Studio IDE:
- Launch Visual Studio, then make a new project called “Console Application”.
- The Code Editor is where you write your C# code.
- Press F5 or click the “Run” button to start the project’s compilation and execution. The output is shown in a console window after Visual Studio manages the compilation process.
Using the Command Line:
- Any text editor, such as Notepad, can be used to write C# code. It should then be saved with
.cs
extension, such asHelloWorld.cs
. It is suggested that every class be defined in a different file with a name that matches the class name. - To access the directory where you saved the file, open the command prompt and navigate.
- Your code should be compiled as
csc HelloWorld.cs
using the C# compiler (csc.exe
). - A successful compilation will result in the creation of an executable file, such as
HelloWorld.exe
. - Enter the name of the application to launch it:
HelloWorld
.
Because C# treats int
and bool
as completely independent types and removes problematic operators, it makes C++ simpler. It also avoids typical mistakes like using =
instead of ==
in if
statements, which the compiler will detect.