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

PPTX
Windowforms controls c#
PPT
Introduction To C#
PPT
Intro To Programming Concepts
PPTX
Chapter 1 2 - some size factors
PPTX
C# programming language
PPT
Introduction to .NET Framework
PPT
Visula C# Programming Lecture 1
PPTX
C# Tutorial
Windowforms controls c#
Introduction To C#
Intro To Programming Concepts
Chapter 1 2 - some size factors
C# programming language
Introduction to .NET Framework
Visula C# Programming Lecture 1
C# Tutorial

What's hot (20)

PPTX
Windows form application - C# Training
PPTX
Integrated Development Environments (IDE)
PPTX
Introduction To C#
PPS
Vb6.0 Introduction
PPT
Computer programming concepts
PPSX
Algorithm and flowchart
PPT
Shell and its types in LINUX
PPT
Visual studio.net
PPT
ADO .Net
PPTX
software cost factor
PPT
Working with frames
PPT
Asp.net.
PDF
Deployment Diagram
PDF
The pseudocode
PPTX
Software quality assurance
PPTX
Ado.Net Tutorial
PPTX
Lecture 1 introduction to vb.net
PPTX
How to download and install Python - lesson 2
Windows form application - C# Training
Integrated Development Environments (IDE)
Introduction To C#
Vb6.0 Introduction
Computer programming concepts
Algorithm and flowchart
Shell and its types in LINUX
Visual studio.net
ADO .Net
software cost factor
Working with frames
Asp.net.
Deployment Diagram
The pseudocode
Software quality assurance
Ado.Net Tutorial
Lecture 1 introduction to vb.net
How to download and install Python - lesson 2
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)
PPTX
C# 101: Intro to Programming with C#
PDF
CP Handout#5
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)
C# 101: Intro to Programming with C#
CP Handout#5
Ad

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

PPTX
c#.pptx
DOCX
csharp.docx
DOCX
Chapter 3(1)
PPTX
C# lecture 1: Introduction to Dot Net Framework
DOCX
Uniti classnotes
PPTX
Structure of a C# Program
PPT
490450755-Chapter-2.ppt
PPT
490450755-Chapter-2.ppt
PPTX
C programming
PDF
PDF
Learning the C Language
PPT
Introduction To Programming subject1.ppt
PPTX
Unit-2_Getting Started With ‘C’ Language (3).pptx
PPTX
C Programming UNIT 1.pptx
PPT
Overview of c#
PPTX
Unit-1 (introduction to c language).pptx
PDF
C class basic programming 1 PPT mayanka (1).pdf
PPT
Basics1
PPTX
Intro in understanding to C programming .pptx
PPTX
Intro in understanding to C programming .pptx
c#.pptx
csharp.docx
Chapter 3(1)
C# lecture 1: Introduction to Dot Net Framework
Uniti classnotes
Structure of a C# Program
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
C programming
Learning the C Language
Introduction To Programming subject1.ppt
Unit-2_Getting Started With ‘C’ Language (3).pptx
C Programming UNIT 1.pptx
Overview of c#
Unit-1 (introduction to c language).pptx
C class basic programming 1 PPT mayanka (1).pdf
Basics1
Intro in understanding to C programming .pptx
Intro in understanding to C programming .pptx

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)

PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java ‱ Spring Boot ‱ Ka...
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
PDF
Reimagining Insurance: Connected Data for Confident Decisions.pdf
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
PDF
creating-agentic-ai-solutions-leveraging-aws.pdf
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
PPTX
Comunidade Salesforce SĂŁo Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
This slide provides an overview Technology
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
PDF
Software Development Methodologies in 2025
 
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
PDF
REPORT: Heating appliances market in Poland 2024
 
Smarter Business Operations Powered by IoT Remote Monitoring
CroxyProxy Instagram Access id login.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java ‱ Spring Boot ‱ Ka...
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Chapter 2 Digital Image Fundamentals.pdf
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
Reimagining Insurance: Connected Data for Confident Decisions.pdf
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
A Day in the Life of Location Data - Turning Where into How.pdf
creating-agentic-ai-solutions-leveraging-aws.pdf
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Comunidade Salesforce SĂŁo Paulo - Desmistificando o Omnistudio (Vlocity)
This slide provides an overview Technology
GamePlan Trading System Review: Professional Trader's Honest Take
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
Software Development Methodologies in 2025
 
Enable Enterprise-Ready Security on IBM i Systems.pdf
REPORT: Heating appliances market in Poland 2024
 

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