C# is a modern programming language developed by Anders
Hejlsberg and his team during the development of .Net Framework and approved by
Ecma and ISO.
C# is designed for Common Language Infrastructure (CLI),
which consists of the executable code and runtime environment that allows use
of various high-level languages to be used on different computer platforms.
The following reasons make C# a widely used professional
language:
Ø
Modern, general-purpose programming language
Ø
Structured language
Ø
Object oriented
Ø
Component oriented
Ø
Easy to learn
Ø
It produces efficient programs
Ø
It can be compiled on a variety of computer
platforms
Ø
Part of .Net Framework
Strong Programming Features of C#
Although C# constructs closely follow traditional high-level
languages C and C++ and being an object-oriented programming language, it has
strong resemblance with Java, it has numerous strong programming features that
make it endearing to multitude of programmers worldwide.
Following is the list of few important features:
Ø
Automatic Garbage Collection
Ø
Standard
Library
Ø
Boolean Conditions
Ø
Assembly Versioning
Ø
Properties and Events
Ø
Delegates and Events Management
Ø
Conditional Compilation
Ø
Indexers
Ø
Simple Multithreading
Ø
Easy-to-use Generics
Ø
LINQ and Lambda Expressions
C# - Program Structure
A C# program basically consists of the following parts:
Ø
Namespace declaration
Ø
A class
Ø
Class methods
Ø
Class
attributes
Ø
A Main
method
Ø
Statements & Expressions
Ø
Comments
While writing a C# Programs, we have to keep following
points in mind
Ø
C# is case sensitive.
Ø
All statements and expression must end with a
semicolon (;).
Ø
The program execution starts at the Main method.
Ø
File name could be different from the class
name.
Let us Consider Simple C# Program to understand more about
structure and how to write C# program
using
System;
namespace
PrintMyNameApp
{
class PrintMyName
{
static void Main(string[] args)
{
/* my first program in
C# */
Console.WriteLine("My Name is Dhananjay");
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces
the following result:
“My Name is Dhananjay”
Now discuss various parts of the above program:
Ø
The first line of the program using System; -using is a
keyword and System is namespace. The
using keyword is used to include the System namespace in the program.
A program generally has multiple using statements.
Ø
The next line has the namespace declaration. A
namespace is a collection of classes.
The PrintMyNameApp namespace
contains the class PrintMyName.
Ø
The next line has a class declaration, the class
PrintMyName contains the data and method
definitions that your program uses. Classes generally would contain more than
one method. Methods define the behavior of the class.
However, the PrintMyName class has
only one method Main.
Ø
The next line defines the Main method, which is
the entry point for all C# programs. The Main method states what the class will
do when executed.
Ø
The next line /*...*/ will be ignored by the
compiler and it has been put to add additional comments in the program.
Ø
The Main method specifies its behavior with the
statement Console.WriteLine("My Name is Dhananjay");
Ø
WriteLine
is a method of the Console class defined in the System namespace. This
statement causes the message " My Name is Dhananjay!" to be displayed
on the screen.
Ø
The last line Console.ReadKey(); which makes the program wait for a key press
and it prevents the screen from running and closing quickly.
Compile & Execute a C# Program:
C# Program can be Compile from Visual Studio IDE OR Using
Command Line
Steps For compiling and executing C# programs using Visual
Studio IDE
Ø
Start Visual Studio.
Ø
From the
menu bar, choose File, New, Project.
Ø
Choose
Visual C# from templates, Windows, Console Application.
Ø
Specify a
name for your project, and then choose the OK button.
Ø
The new
project appears in Solution Explorer.
Ø
Write
code in the Code Editor.
Click the Run button or the F5 key
to run the project.
Steps For compiling and executing C# programs using Command
Line
Ø
Open a text editor and add the above-mentioned
code.
Ø
Save the file as PrintMyName.cs
Ø
Open the command prompt tool and go to the
directory where you saved the file.
Ø
Type csc PrintMyName.cs
and press enter to compile your code.
Ø
If there
are no errors in your code, the command prompt will take you to the next line
and would generate PrintMyName.exe executable file, where your code file is
saved.
Ø
Next, type PrintMyName to execute your
program.
Ø
You will be able to see "My Name is
Dhananjay" printed on the screen.
No comments:
Post a Comment