SlideShare a Scribd company logo
Learn C# Programming
Program Structure & Basic Syntax
Eng Teong Cheah
Microsoft MVP in Visual Studio &
Development Technologies
Agenda
•Program Structure
•Basic Syntax
C# - Program Structure
C# - Program Structure
Before we study basic building blocks of the C# programming
language, let us look at a bare minimum C# program structure so
that we can take it as a reference in upcoming chapters.
C# - Program Structure
Create Hello World Program
A C# program consists of the following parts:
- Namespace declaration
- A class
- Class methods
- Class attributes
- A Main method
- Statements and Expressions
- Comments
C# - Program Structure
Let us look at a simple code that prints the words “Hello World”:
When this code is compiled and executed, it produces the following result:
Hello World
C# - Program Structure
Let us look at the various parts of the given program:
The first line of the program using System; - the using keyword is
used to include the System namespace in the program. A program
generally has multiple using statements.
C# - Program Structure
Let us look at the various parts of the given program:
The next line has the namespace declaration. A namespace is a
collection of classes. The HelloWorldApplication namespace
contains the class HelloWorld.
C# - Program Structure
Let us look at the various parts of the given program:
The next line has a class declaration, the class HelloWorld contains
the data and method definitions that your program uses. Classes
generally contain multiple methods. Methods define the behaviour
of the class. However, the HelloWorld class has only one method
Main.
C# - Program Structure
Let us look at the various parts of the given program:
The next line defines the Main method, which is the entry point for
all C# programs. The Main method states what the class does
when executed.
C# - Program Structure
Let us look at the various parts of the given program:
The next line /*..*/ is ignored by the compiler and it is put to add
comments in the program.
C# - Program Structure
Let us look at the various parts of the given program:
The Main method specified its behaviour with the statement
Console.WriteLine (“Hello World”);
WriteLine is a method of the Console class defined in the System
namespace. This statement causes the message “Hello World” to
be displayed on the screen.
C# - Program Structure
Let us look at the various parts of the given program:
The last line Console.ReadKey(); is for the VS.NET Users. This
makes the program wait for a key press and it prevents the screen
from running and closing quickly when the program is launched
from Visual Studio .NET.
C# - Program Structure
It is worth to note the following points:
- C# is case sensitive
- All statements and expression must end with a semicolon(;)
- The program execution starts at the Main method.
- Unlike Java, program file name could be different from the
class name.
Demo
C# - Basic Syntax
C# - Basic Syntax
C# is an object-oriented programming language. In Object-
Oriented Programming methodology, a program consists of
various objects that interact with each other by means of
actions. The actions that an object may take are called
methods. Objects of the same kind are said to have the
same type or, are said to be in the same class.
C# - Basic Syntax
For example, let us consider a Rectangle object. It has
attributes such as length and width. Depending upon the
design, it may need ways for accepting the values of these
attributes, calculating the area, and displaying details.
Lets us look at implementation of a Rectangle and discuss
C# basic syntax:
C# - Basic Syntax
Length: 4.5
Width: 3.5
Area: 15.75
When the code is compiled and
executed, it produces the
following result:
C# - Basic Syntax
The using Keyword
The first statement in any C# program is
using System;
The using keyword is used for including the
namespace in the program. A program can include
multiple using statements.
C# - Basic Syntax
The class Keyword
The class keyword is used for declaring a class.
C# - Basic Syntax
Member Variables
Variables are attributes or data members of a class,
used for storing data. In the preceding program, the
Rectangle class has two member variables named
length and width.
C# - Basic Syntax
Member Functions
Functions are set of statements that perform a specific
task. The member functions of a class are declared
within the class. Our sample class Rectangle contains
three member functions: AcceptDetails, GetArea and
Display.
C# - Basic Syntax
Instantiating a Class
In the preceding program, the class ExecuteRectangle
contains the Main() method and instantiates the
Rectangle class.
C# - Basic Syntax
Identifiers
An identifier is a name used to identify a class, variable, function,
or any other user-defined item. The basic rules for naming
classes in C# are as follows:
- A name must begin with a letter that could be followed by a
sequence of letters, digits (0-9) or underscore. The first
character in an identifier cannot be a digit.
C# - Basic Syntax
Identifiers
An identifier is a name used to identify a class, variable, function,
or any other user-defined item. The basic rules for naming
classes in C# are as follows:
-It must not contain any embedded space or symbol such as ? -
+ ! @ # % ^ & * ( ) [ ] { } . ; : “ ‘ / and . However, an underscore
(_) can be used.
C# - Basic Syntax
Identifiers
An identifier is a name used to identify a class, variable, function,
or any other user-defined item. The basic rules for naming
classes in C# are as follows:
- It should not be a C# keyword.
C# - Basic Syntax
C# Keywords
Keywords are reserved words predefined to the C# compiler.
Theses keywords cannot be used as identifiers. However, if you
want to use these keywords as identifiers, you may prefix the
keyword with the @ character.
In C#, some identifiers have special meaning in context of code,
such as get and set are called contextual keywords.
C# - Basic Syntax
The following table lists the
reserved keywords and
contextual keywords in C#:
C# - Basic Syntax
The following table lists the reserved keywords and contextual
keywords in C#:
Demo
Related Content
•TutorialsPoint
www.tutorialspoint.com
Thank You

More Related Content

PDF
Java Virtual Machine - Internal Architecture
PPTX
Objective c slide I
PPTX
C# 101: Intro to Programming with C#
PPTX
System Programming Overview
PDF
Introduction to basics of java
PPT
Intro To Programming Concepts
PDF
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
Java Virtual Machine - Internal Architecture
Objective c slide I
C# 101: Intro to Programming with C#
System Programming Overview
Introduction to basics of java
Intro To Programming Concepts
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi

What's hot (20)

PDF
Visual Basic IDE Introduction
PPTX
Programming Paradigm & Languages
PPT
History of c++
PPTX
PPTX
C_Programming_Notes_ICE
PPT
Path testing, data flow testing
PPTX
COMPUTER PROGRAMMING
PPT
Lecture 1
PPTX
Introduction to java
PPSX
Introduction to .net framework
PPT
10 software maintenance
PPSX
Introduction to Java
PPT
C sharp
PPTX
Introduction to JAVA
PPTX
Full stack development
PDF
P code
PPTX
C# language
PPT
Introduction to Compiler design
PPTX
Code review
PPTX
Editor structure
Visual Basic IDE Introduction
Programming Paradigm & Languages
History of c++
C_Programming_Notes_ICE
Path testing, data flow testing
COMPUTER PROGRAMMING
Lecture 1
Introduction to java
Introduction to .net framework
10 software maintenance
Introduction to Java
C sharp
Introduction to JAVA
Full stack development
P code
C# language
Introduction to Compiler design
Code review
Editor structure
Ad

Viewers also liked (20)

PPTX
Xamarin - First Application
PDF
Learn C# Programming - Data Types & Type Conversion
PPTX
Introduction of C#
PPT
Difference between Java and c#
PPT
C#/.NET Little Wonders
PPTX
.NET and C# Introduction
PDF
Learn C# Programming - Variables & Constants
PPT
Agile Methodologies
PDF
Xamarin: Namespace and Classes
PDF
Xamarin: Branching and Looping
PDF
Xamarin: C# Methods
PPTX
Academy PRO: Unity 3D. Scripting
PDF
Unity 3d scripting tutorial
PDF
Xamarin: Inheritance and Polymorphism
PPT
Chapter 13 - Inheritance and Polymorphism
PDF
CP Handout#2
PPTX
Agile Retrospectives
PPT
8.3 program structure (1 hour)
PDF
CP Handout#5
PPTX
Constructs (Programming Methodology)
Xamarin - First Application
Learn C# Programming - Data Types & Type Conversion
Introduction of C#
Difference between Java and c#
C#/.NET Little Wonders
.NET and C# Introduction
Learn C# Programming - Variables & Constants
Agile Methodologies
Xamarin: Namespace and Classes
Xamarin: Branching and Looping
Xamarin: C# Methods
Academy PRO: Unity 3D. Scripting
Unity 3d scripting tutorial
Xamarin: Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
CP Handout#2
Agile Retrospectives
8.3 program structure (1 hour)
CP Handout#5
Constructs (Programming Methodology)
Ad

Similar to Learn C# programming - Program Structure & Basic Syntax (20)

PPTX
DOCX
csharp.docx
PPTX
Structure of a C# Program
PPTX
C# lecture 1: Introduction to Dot Net Framework
PDF
C# c# for beginners crash course master c# programming fast and easy today
PPTX
CSharp Presentation
PPTX
A POWERPOINT PRESENTATION ABOUT INTRODUCTION TO C#
PPTX
introduction to c #
PDF
overview of c#
PDF
C For Beginners Crash Course Master C Programming Fast And Easy Today 1st Edi...
PPSX
Microsoft C# programming basics
PPTX
Cordovilla
PPTX
CSharp Programming.pptx
PDF
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
PPTX
Chapter 2 c#
PPTX
Introduction to C#
PPTX
Csharp introduction
PPT
PPT
Presentatiooooooooooon00000000000001.ppt
csharp.docx
Structure of a C# Program
C# lecture 1: Introduction to Dot Net Framework
C# c# for beginners crash course master c# programming fast and easy today
CSharp Presentation
A POWERPOINT PRESENTATION ABOUT INTRODUCTION TO C#
introduction to c #
overview of c#
C For Beginners Crash Course Master C Programming Fast And Easy Today 1st Edi...
Microsoft C# programming basics
Cordovilla
CSharp Programming.pptx
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
Chapter 2 c#
Introduction to C#
Csharp introduction
Presentatiooooooooooon00000000000001.ppt

More from Eng Teong Cheah (20)

PDF
Modern Cross-Platform Apps with .NET MAUI
PDF
Efficiently Removing Duplicates from a Sorted Array
PDF
Monitoring Models
PDF
Responsible Machine Learning
PDF
Training Optimal Models
PDF
Deploying Models
PDF
Machine Learning Workflows
PDF
Working with Compute
PDF
Working with Data
PDF
Experiments & TrainingModels
PDF
Automated Machine Learning
PDF
Getting Started with Azure Machine Learning
PDF
Hacking Containers - Container Storage
PDF
Hacking Containers - Looking at Cgroups
PDF
Hacking Containers - Linux Containers
PDF
Data Security - Storage Security
PDF
Application Security- App security
PDF
Application Security - Key Vault
PDF
Compute Security - Container Security
PDF
Compute Security - Host Security
Modern Cross-Platform Apps with .NET MAUI
Efficiently Removing Duplicates from a Sorted Array
Monitoring Models
Responsible Machine Learning
Training Optimal Models
Deploying Models
Machine Learning Workflows
Working with Compute
Working with Data
Experiments & TrainingModels
Automated Machine Learning
Getting Started with Azure Machine Learning
Hacking Containers - Container Storage
Hacking Containers - Looking at Cgroups
Hacking Containers - Linux Containers
Data Security - Storage Security
Application Security- App security
Application Security - Key Vault
Compute Security - Container Security
Compute Security - Host Security

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
Teaching material agriculture food technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
cuic standard and advanced reporting.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Approach and Philosophy of On baking technology
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Machine learning based COVID-19 study performance prediction
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Encapsulation theory and applications.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Cloud computing and distributed systems.
Understanding_Digital_Forensics_Presentation.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Teaching material agriculture food technology
MYSQL Presentation for SQL database connectivity
cuic standard and advanced reporting.pdf
Network Security Unit 5.pdf for BCA BBA.
Approach and Philosophy of On baking technology
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectroscopy.pptx food analysis technology
Machine learning based COVID-19 study performance prediction
The AUB Centre for AI in Media Proposal.docx
Encapsulation theory and applications.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Empathic Computing: Creating Shared Understanding
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Cloud computing and distributed systems.

Learn C# programming - Program Structure & Basic Syntax

  • 1. Learn C# Programming Program Structure & Basic Syntax Eng Teong Cheah Microsoft MVP in Visual Studio & Development Technologies
  • 3. C# - Program Structure
  • 4. C# - Program Structure Before we study basic building blocks of the C# programming language, let us look at a bare minimum C# program structure so that we can take it as a reference in upcoming chapters.
  • 5. C# - Program Structure Create Hello World Program A C# program consists of the following parts: - Namespace declaration - A class - Class methods - Class attributes - A Main method - Statements and Expressions - Comments
  • 6. C# - Program Structure Let us look at a simple code that prints the words “Hello World”: When this code is compiled and executed, it produces the following result: Hello World
  • 7. C# - Program Structure Let us look at the various parts of the given program: The first line of the program using System; - the using keyword is used to include the System namespace in the program. A program generally has multiple using statements.
  • 8. C# - Program Structure Let us look at the various parts of the given program: The next line has the namespace declaration. A namespace is a collection of classes. The HelloWorldApplication namespace contains the class HelloWorld.
  • 9. C# - Program Structure Let us look at the various parts of the given program: The next line has a class declaration, the class HelloWorld contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behaviour of the class. However, the HelloWorld class has only one method Main.
  • 10. C# - Program Structure Let us look at the various parts of the given program: The next line defines the Main method, which is the entry point for all C# programs. The Main method states what the class does when executed.
  • 11. C# - Program Structure Let us look at the various parts of the given program: The next line /*..*/ is ignored by the compiler and it is put to add comments in the program.
  • 12. C# - Program Structure Let us look at the various parts of the given program: The Main method specified its behaviour with the statement Console.WriteLine (“Hello World”); WriteLine is a method of the Console class defined in the System namespace. This statement causes the message “Hello World” to be displayed on the screen.
  • 13. C# - Program Structure Let us look at the various parts of the given program: The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.
  • 14. C# - Program Structure It is worth to note the following points: - C# is case sensitive - All statements and expression must end with a semicolon(;) - The program execution starts at the Main method. - Unlike Java, program file name could be different from the class name.
  • 15. Demo
  • 16. C# - Basic Syntax
  • 17. C# - Basic Syntax C# is an object-oriented programming language. In Object- Oriented Programming methodology, a program consists of various objects that interact with each other by means of actions. The actions that an object may take are called methods. Objects of the same kind are said to have the same type or, are said to be in the same class.
  • 18. C# - Basic Syntax For example, let us consider a Rectangle object. It has attributes such as length and width. Depending upon the design, it may need ways for accepting the values of these attributes, calculating the area, and displaying details. Lets us look at implementation of a Rectangle and discuss C# basic syntax:
  • 19. C# - Basic Syntax Length: 4.5 Width: 3.5 Area: 15.75 When the code is compiled and executed, it produces the following result:
  • 20. C# - Basic Syntax The using Keyword The first statement in any C# program is using System; The using keyword is used for including the namespace in the program. A program can include multiple using statements.
  • 21. C# - Basic Syntax The class Keyword The class keyword is used for declaring a class.
  • 22. C# - Basic Syntax Member Variables Variables are attributes or data members of a class, used for storing data. In the preceding program, the Rectangle class has two member variables named length and width.
  • 23. C# - Basic Syntax Member Functions Functions are set of statements that perform a specific task. The member functions of a class are declared within the class. Our sample class Rectangle contains three member functions: AcceptDetails, GetArea and Display.
  • 24. C# - Basic Syntax Instantiating a Class In the preceding program, the class ExecuteRectangle contains the Main() method and instantiates the Rectangle class.
  • 25. C# - Basic Syntax Identifiers An identifier is a name used to identify a class, variable, function, or any other user-defined item. The basic rules for naming classes in C# are as follows: - A name must begin with a letter that could be followed by a sequence of letters, digits (0-9) or underscore. The first character in an identifier cannot be a digit.
  • 26. C# - Basic Syntax Identifiers An identifier is a name used to identify a class, variable, function, or any other user-defined item. The basic rules for naming classes in C# are as follows: -It must not contain any embedded space or symbol such as ? - + ! @ # % ^ & * ( ) [ ] { } . ; : “ ‘ / and . However, an underscore (_) can be used.
  • 27. C# - Basic Syntax Identifiers An identifier is a name used to identify a class, variable, function, or any other user-defined item. The basic rules for naming classes in C# are as follows: - It should not be a C# keyword.
  • 28. C# - Basic Syntax C# Keywords Keywords are reserved words predefined to the C# compiler. Theses keywords cannot be used as identifiers. However, if you want to use these keywords as identifiers, you may prefix the keyword with the @ character. In C#, some identifiers have special meaning in context of code, such as get and set are called contextual keywords.
  • 29. C# - Basic Syntax The following table lists the reserved keywords and contextual keywords in C#:
  • 30. C# - Basic Syntax The following table lists the reserved keywords and contextual keywords in C#:
  • 31. Demo