0% found this document useful (0 votes)
5 views15 pages

Unit 1 Presentation

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 15

M107-

Introduction to

C#
Programming
Course Information
Number of Credits-3

Assessment:
TMA - 20%
MTA - 30%
Final - 50%
Introduction to C#
History of C#
• Developed by Microsoft approved by Ecma and
ISO

• Developed by Anders Hejlsberg and his team


during the development of .Net Framework.

• Based on Java and C++

• Cross-development with Visual Basic, Visual C++,


and many other .NET languages.
About C#
The following reasons make C#, a widely used professional
language:

• Modern, general purpose programming language.


• Object oriented.
• Component oriented.
• Easy to learn.
• Structured language.
• It produces efficient programs.
• It can be compiled on a variety of computer platforms.
• Part of .Net Framework.
A SIMPLE C# PROGRAM
class SampleOne
{
public static void Main()
{
System.Console.WriteLine(“ C# is sharper than C++.");
}
}

Above 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. C# is a true object-oriented
language and therefore, 'everything' must be placed
inside a class.
class is a keyword and declares that a new class definition
follows.
SampleOne is a C# identifier that specifies the name of
the class to be defined.

A class is a template for what an object looks like and how


it behaves.
(Details of class and object will be covered in Unit 8)
The Braces
C# is a block-structured language, meaning, code blocks
are always enclosed by braces { and }.
Therefore, every class definition 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
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.


The Main Method
Public  The keyword public is an access modifier 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).
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++.

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++ to the


screen.

Every C# statement must end with a semicolon (;).


NAMESPACES
Let us consider the output statement of the previous sample
program again:

System.Console.WriteLine ();

In this, note that System is the namespace (scope) in which the

Console class is located.

A class in a namespace can be accessed using the dot operator

(.) as illustrated in the statement above.

Namespaces are the way C# segregates the .NET library

classes into reasonable groupings.


ADDING COMMENTS
C# permits two types of comments, namely,
• Single-line comments
• Multiline comments

Single-line comments begin with a double backslash ( // )


symbol and terminate at the end of the line. Everything
after the // on a line is considered a comment.
Multi-line comments starts with /* characters and
terminates with */ as shown in the beginning of the
program. If we want to use multiple lines for a comment,
this type is used.
Points to remember

• C# is case sensitive.
• All statements and expression must end
with a semicolon (;).

• The program execution starts at the Main


method.

• Unlike Java, file name could be different


from the class name.
Thank you….

You might also like