C# is an object oriented programming language. C# is created by Anders Hejlsberg and his team at Microsoft Corporation, USA. It borrows concepts and styles from Java and C++.
Program Structure
A C# program may contain a number coding blocks as shown in the following figure.
The documentation section consists of a set of comments giving the name of the program, the author, date and other details.
The following is a simple C# program that prints a string on the screen.
/* This is a simple C# program. Call this program Example.cs */
using System;
class ClsWelcome
{
static void Main()
{
Console.WriteLine("Welcome to C# Programming");
}
}
The program begins with the following lines:
/* This is a simple C# program. Call this program Example.cs */
This is a comment. The contents of a comment are ignored by the compiler. Instead, a comment describes or explains the operation of the program to anyone who is reading its source code. The next line in the program is
using System;
The namespace used by the program is System, which is the namespace reserved for items associated with the .NET Framework class library, which is the library used by C#.
The using keyword simply states that the program is using the names in the given namespace. The next line of code in the program is shown here:
class ClsWelcome {
This line uses the keyword class to declare that a new class is being defined. ClsWelcome is the name of the class. The class definition begins with the opening curly brace ({) and ends with the closing curly brace (}). The elements between the two braces are members of the class.
The next line of code is shown here:
static void Main() {
This line begins the Main( ) method. This is the line at which the program will begin executing. All C# applications begin execution by calling Main( ). The line begins with the keyword static. A method that is modified by static can be called before an object of its class has been created. This is necessary because Main( ) is called at program startup. The keyword void indicates that Main( ) does not return a value. The empty parentheses that follow Main indicate that no information is passed to Main( ). The last character on the line is the {. This signals the start of Main( )’s body. All of the code that comprises a method will occur between the method’s opening curly brace and its closing curly brace.
The next line of code is
Console.WriteLine("Welcome to C# Programming");
This line outputs the string “Welcome to C# Programming” followed by a new line on the screen. Output is actually accomplished by the built-in method WriteLine( ). In this case, WriteLine( ) displays the string that is passed to it. Information that is passed to a method is called an argument. The line begins with Console, which is the name of a predefined class that supports console I/O. WriteLine( ) is a member of the Console class. Statements in C# end with a semicolon. The exception to this rule are blocks, which begin with a { and end with a }. Blocks provide a mechanism for grouping statements. The first } in the program ends Main( ), and the last } ends the ClsWelcome class definition.
Executing a C# Program
stay tuned.. coming up in the next post...
Program Structure
A C# program may contain a number coding blocks as shown in the following figure.
C# Program structure |
The using directive section will include all those namespaces that contain classes required by the application. An interface is similar to a class but contains only abstract members.
Interfaces are used when we want to implement the concept of multiple inheritance in a program. A C# program may contain multiple class definitions.
Classes are the primary and essential elements of a C# program.
Since every C# application program requires a Main method as its starting point, the class containing the Main is the essential part of the program.
The Main method creates objects of various classes and establishes communications between them.
C# is case-sensitive language. The extension of a C# program file is .cs
A Simple C# Program
The following is a simple C# program that prints a string on the screen.
/* This is a simple C# program. Call this program Example.cs */
using System;
class ClsWelcome
{
static void Main()
{
Console.WriteLine("Welcome to C# Programming");
}
}
The program begins with the following lines:
/* This is a simple C# program. Call this program Example.cs */
This is a comment. The contents of a comment are ignored by the compiler. Instead, a comment describes or explains the operation of the program to anyone who is reading its source code. The next line in the program is
using System;
The namespace used by the program is System, which is the namespace reserved for items associated with the .NET Framework class library, which is the library used by C#.
The using keyword simply states that the program is using the names in the given namespace. The next line of code in the program is shown here:
class ClsWelcome {
This line uses the keyword class to declare that a new class is being defined. ClsWelcome is the name of the class. The class definition begins with the opening curly brace ({) and ends with the closing curly brace (}). The elements between the two braces are members of the class.
The next line of code is shown here:
static void Main() {
This line begins the Main( ) method. This is the line at which the program will begin executing. All C# applications begin execution by calling Main( ). The line begins with the keyword static. A method that is modified by static can be called before an object of its class has been created. This is necessary because Main( ) is called at program startup. The keyword void indicates that Main( ) does not return a value. The empty parentheses that follow Main indicate that no information is passed to Main( ). The last character on the line is the {. This signals the start of Main( )’s body. All of the code that comprises a method will occur between the method’s opening curly brace and its closing curly brace.
The next line of code is
Console.WriteLine("Welcome to C# Programming");
This line outputs the string “Welcome to C# Programming” followed by a new line on the screen. Output is actually accomplished by the built-in method WriteLine( ). In this case, WriteLine( ) displays the string that is passed to it. Information that is passed to a method is called an argument. The line begins with Console, which is the name of a predefined class that supports console I/O. WriteLine( ) is a member of the Console class. Statements in C# end with a semicolon. The exception to this rule are blocks, which begin with a { and end with a }. Blocks provide a mechanism for grouping statements. The first } in the program ends Main( ), and the last } ends the ClsWelcome class definition.
Executing a C# Program
stay tuned.. coming up in the next post...
No comments:
Post a Comment