C# (pronounced “see sharp” or “C Sharp”) is one of many .NET programming languages. It is object-oriented and allows you to build reusable components for a wide variety of application types Microsoft introduced C# on June 26th, 2000 and it became a v1.0 product on Feb 13th 2002
1. @ 2010 Tata McGraw-Hill Education
1
Education
Overview of C#Overview of C#
2. @ 2010 Tata McGraw-Hill Education
2
Education
‘C #’ seems a strange name for a modern programming language. Perhaps
Microsoft named their new language ‘C sharp’ because they wanted it to be
better, smarter and ‘sharper’ than its ancestors C and C++. C# is designed to
bring rapid development to C++ programmers without sacrificing the power
and control that have been the hallmarks of C and C++. C# is the only
language designed specially for the .NET platform which provides tools and
services that fully exploit both computing and communications.
C# can be used to develop two categories of programs, namely
Executable application programs and
Component libraries
3. @ 2010 Tata McGraw-Hill Education
3
Education
as illustrated in Fig. 3.1. Executable
programs are written to carry out certain
tasks and require the method Main in one
of the classes. In contrast, component
libraries do not require
a Main declaration because they are not
standalone application programs. They are
written for use by other applications. This
concept is something similar to applets and
application programs in JavD.
4. @ 2010 Tata McGraw-Hill Education
4
Education
Above program is the simplest of all C# programs. Nevertheless, it brings out
many salient features of the language. Let us therefore discuss the program line
by line and understand the unique features that constitute a C# program.
Class Declaration: The first line
class SampleOne
declares a class, which is an object-oriented construct. As stated earlier, C# is a
true object-oriented language and therefore, ‘everything’
5. @ 2010 Tata McGraw-Hill Education
5
Education
The Braces: C# is a block-structured language, meaning code blocks are always
enclosed by braces { and }. Therefore, every class defi nition in C# begins with an
opening brace ‘{’and ends with a corresponding closing brace ‘}’ that appears in the
last line of the program. This is similar to class constructs of Java and C++. Note that
there is no semicolon after the closing brace.
The Main Method
The third line
public static void Main( )
defines a method named Main. Every C# executable program must include the
Main( ) method in one of the classes. This is the ‘starting point’ for executing the
program. A C# application can have any number of classes but ‘only one’ class can
have the Main method to initiate the execution. Note that C# component libraries will
not use the Main method at all. This line contains a number of keywords: public,
static and void. This is very similar to the main of C++ and JavD. In contrast to
Java and C++, Main has a capital, not lowercase M. The meaning and purpose of
these keywords are give below:
6. @ 2010 Tata McGraw-Hill Education
6
Education
Public: The keyword public is an access modifi er that tells the C# compiler that
the Main method is accessible by anyone
Static: The keyword static declares that the Main method is a global one and can
be called without creating an instance of the class. The compiler stores the address of
the method as the entry point and uses this information to begin execution before any
objects are created.
Void: The keyword void is a type modifier that states that the Main method does
not return any value (but simply prints some text to the screen).
Note: It is not essential to declare Main as public. However, this is how Visual
Studio 2005 declares it. Also, many authors prefer this approach. We also therefore
declare Main as public in our examples. It is likely that some authors declare it
without the public specifier.
7. @ 2010 Tata McGraw-Hill Education
7
Education
The Output Line
The only executable statement in the program is
System.Console.WriteLine(“C# is sharper than C++.”);
This has a striking resemblance to the output statement of Java and similar to the
printf ( ) of C or cout<< of C++. Since C# is a pure object-oriented language,
every method should be part of an object. The WriteLine method is a static
method of the Console class, which is located in the namespace System. This
line prints the string
C# is sharper than C++.
8. @ 2010 Tata McGraw-Hill Education
8
Education
Note that the first statement in the program is using System;
This tells the compiler to look in the System library for unresolved class names. Note that we
have not used the System prefix to the Console class in the output line. When the compiler
parses the Console.WriteLine method, it will understand that the method is undefined.
However, it will then search through the namespaces specified in using directives and,
upon finding the method in the System namespace, will compile the code without any complaint.
9. @ 2010 Tata McGraw-Hill Education
9
Education
Program 3.3 returns an integer-type value to the system. The value returned serves as
the program’s termination status code. The purpose of this code is to allow
communication of success or failure to the execution environment.
17. @ 2010 Tata McGraw-Hill Education
17
Education
PROGRAM STRUCTURE
An executable C# program may contain a number coding blocks as shown in
Fig. 3.3. The documentation section consists of a set of comments giving the
name of the program, the author, date and other details, which the programmer
(or other users) may like to use at a later stage. Comments must explain the
Why and what of classes, and the
How of algorithms