SlideShare a Scribd company logo
@ 2010 Tata McGraw-Hill Education
1
Education
Overview of C#Overview of C#
@ 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
@ 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.
@ 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’
@ 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:
@ 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.
@ 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++.
@ 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.
@ 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.
@ 2010 Tata McGraw-Hill Education
10
Education
@ 2010 Tata McGraw-Hill Education
11
Education
@ 2010 Tata McGraw-Hill Education
12
Education
@ 2010 Tata McGraw-Hill Education
13
Education
@ 2010 Tata McGraw-Hill Education
14
Education
@ 2010 Tata McGraw-Hill Education
15
Education
@ 2010 Tata McGraw-Hill Education
16
Education
@ 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
@ 2010 Tata McGraw-Hill Education
18
Education
PROGRAM CODING STYLE

More Related Content

What's hot

What's hot (20)

C language
C languageC language
C language
 
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTTC programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
 
C language
C languageC language
C language
 
C notes
C notesC notes
C notes
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
Tokens_C
Tokens_CTokens_C
Tokens_C
 
C language
C languageC language
C language
 
Programming in c notes
Programming in c notesProgramming in c notes
Programming in c notes
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Notes of c programming 1st unit BCA I SEM
Notes of c programming  1st unit BCA I SEMNotes of c programming  1st unit BCA I SEM
Notes of c programming 1st unit BCA I SEM
 
Uniti classnotes
Uniti classnotesUniti classnotes
Uniti classnotes
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C languaGE UNIT-1
C languaGE UNIT-1C languaGE UNIT-1
C languaGE UNIT-1
 
C programming presentation for university
C programming presentation for universityC programming presentation for university
C programming presentation for university
 
Features of c language 1
Features of c language 1Features of c language 1
Features of c language 1
 
Chapter2
Chapter2Chapter2
Chapter2
 
Book ppt
Book pptBook ppt
Book ppt
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
Features of c
Features of cFeatures of c
Features of c
 
Introduction of C++ By Pawan Thakur
Introduction of C++ By Pawan ThakurIntroduction of C++ By Pawan Thakur
Introduction of C++ By Pawan Thakur
 

Similar to Overview of c#

Migrating From Cpp To C Sharp
Migrating From Cpp To C SharpMigrating From Cpp To C Sharp
Migrating From Cpp To C SharpGanesh Samarthyam
 
C language introduction geeksfor geeks
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeksAashutoshChhedavi
 
Learn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxLearn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxEng Teong Cheah
 
C plus plus for hackers it security
C plus plus for hackers it securityC plus plus for hackers it security
C plus plus for hackers it securityCESAR A. RUIZ C
 
1. Introduction to C++ and brief history
1. Introduction to C++ and brief history1. Introduction to C++ and brief history
1. Introduction to C++ and brief historyAhmad177077
 
C++ language basic
C++ language basicC++ language basic
C++ language basicWaqar Younis
 
C# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net FrameworkC# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net FrameworkDr.Neeraj Kumar Pandey
 
Object Oriented Programming (Advanced )
Object Oriented Programming   (Advanced )Object Oriented Programming   (Advanced )
Object Oriented Programming (Advanced )ayesha420248
 
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...NicheTech Com. Solutions Pvt. Ltd.
 
Lecture-1&2.pdf Visual Programming C# .net framework
Lecture-1&2.pdf Visual Programming C# .net frameworkLecture-1&2.pdf Visual Programming C# .net framework
Lecture-1&2.pdf Visual Programming C# .net frameworkAbdullahNadeem78
 
Unit 1 of c++ part 1 basic introduction
Unit 1 of c++ part 1 basic introductionUnit 1 of c++ part 1 basic introduction
Unit 1 of c++ part 1 basic introductionAKR Education
 
Book management system
Book management systemBook management system
Book management systemSHARDA SHARAN
 

Similar to Overview of c# (20)

Migrating From Cpp To C Sharp
Migrating From Cpp To C SharpMigrating From Cpp To C Sharp
Migrating From Cpp To C Sharp
 
C tutorials
C tutorialsC tutorials
C tutorials
 
C AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDYC AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDY
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
C language introduction geeksfor geeks
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeks
 
Learn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxLearn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic Syntax
 
C plus plus for hackers it security
C plus plus for hackers it securityC plus plus for hackers it security
C plus plus for hackers it security
 
1. Introduction to C++ and brief history
1. Introduction to C++ and brief history1. Introduction to C++ and brief history
1. Introduction to C++ and brief history
 
T2
T2T2
T2
 
C++ language basic
C++ language basicC++ language basic
C++ language basic
 
C sharp chap1
C sharp chap1C sharp chap1
C sharp chap1
 
C-sharping.docx
C-sharping.docxC-sharping.docx
C-sharping.docx
 
Srgoc dotnet_new
Srgoc dotnet_newSrgoc dotnet_new
Srgoc dotnet_new
 
C# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net FrameworkC# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net Framework
 
Object Oriented Programming (Advanced )
Object Oriented Programming   (Advanced )Object Oriented Programming   (Advanced )
Object Oriented Programming (Advanced )
 
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
 
Lecture-1&2.pdf Visual Programming C# .net framework
Lecture-1&2.pdf Visual Programming C# .net frameworkLecture-1&2.pdf Visual Programming C# .net framework
Lecture-1&2.pdf Visual Programming C# .net framework
 
Unit 1 of c++ part 1 basic introduction
Unit 1 of c++ part 1 basic introductionUnit 1 of c++ part 1 basic introduction
Unit 1 of c++ part 1 basic introduction
 
Anagha
AnaghaAnagha
Anagha
 
Book management system
Book management systemBook management system
Book management system
 

Recently uploaded

Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfDr. M. Kumaresan Hort.
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfTamralipta Mahavidyalaya
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxRaedMohamed3
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXMIRIAMSALINAS13
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...Nguyen Thanh Tu Collection
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345beazzy04
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxssuserbdd3e8
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxDenish Jangid
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePedroFerreira53928
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfYibeltalNibretu
 

Recently uploaded (20)

Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 

Overview of c#

  • 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.
  • 10. @ 2010 Tata McGraw-Hill Education 10 Education
  • 11. @ 2010 Tata McGraw-Hill Education 11 Education
  • 12. @ 2010 Tata McGraw-Hill Education 12 Education
  • 13. @ 2010 Tata McGraw-Hill Education 13 Education
  • 14. @ 2010 Tata McGraw-Hill Education 14 Education
  • 15. @ 2010 Tata McGraw-Hill Education 15 Education
  • 16. @ 2010 Tata McGraw-Hill Education 16 Education
  • 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
  • 18. @ 2010 Tata McGraw-Hill Education 18 Education PROGRAM CODING STYLE