SlideShare a Scribd company logo
Learn C# Programming
Variables and Constants
Eng Teong Cheah
Microsoft MVP in Visual Studio &
Development Technologies
Agenda
•Variables
•Constants
C# - Variables
C# - Variables
A variable is nothing but a name given to a storage area that our
programs can manipulate. Each variable in C# has a specific type,
which determines the size and layout of the variable’s memory the
range of values that can be stored within that memory and the set of
operations that can be applied to the variable.
C# - Variables
The basic value types provided in C# can be categorized as:
C# - Variables
C# also allows defining other value types of variable such as
enum and reference types of variables such as class, which
we will cover in subsequent chapters.
C# - Variables
Defining Variables
Syntax for variable definition in C# is:
Here, data_type must be a valid C# data type including char,
int, float, double, or any user-defined data type, and
variable_list may consist of one or more identifier names
separated by commas.
<data_type> <variable_list>;
C# - Variables
Defining Variables
Some valid variable definitions are shown here:
int i, j, k;
char c, ch;
float f, salary;
double d;
You can initialize a variable at the time of definition as:
int i = 100;
C# - Variables
Initializing Variables
Variables are initialized (assigned a value) with an equal sign
followed by a constant expression. The general form of
initialization is:
Variables can be initialized in their declaration. The initializer
consists of an equal sign followed by a constant expression
as:
variable_name = value;
<data_type> <variable_name> = value;
C# - Variables
Initializing Variables
Some examples are:
int d = 3, f = 5; /* initializing d and f. */
byte z = 22; /* initializes z. */
double pi = 3.14159; /*declares an approximation of pi. */
char x = ‘x’;
C# - Variables
Initializing Variables
It is a good programming practice to initialize variables
properly, otherwise sometimes program may produce
unexpected result.
Demo
C# - Variables
Accepting Values from User
The Console class in the System namespace provides
function ReadLine() for accepting input from the user and
store it into a variable.
For example,
int num;
num = Convert.ToInt32(Console.ReadLine());
C# - Variables
Accepting Values from User
The function Convert.ToInt32() converts the data entered by
the user to int data type, because Console.ReadLine()
accepts the data in string format.
C# - Variables
Lvalue and Rvalue Expression in C#:
The are two kinds of expressions in C#:
- lvalue: An expression that is an lvalue may appear as either
the left-hand or right-hand side of an assignment.
- rvalue: AN expression that is an rvalue may appear on the
right-hand but not left-hand side of an assignment.
C# - Variables
Lvalue and Rvalue Expression in C#:
Variables are lvalues and hence they may appear on the left-
hand side of an assignment. Numeric literals are rvalues and
hence they may not be assigned and cannot appear on the
left-hand side. Following is a valid C# statement:
int g =20;
C# - Variables
Lvalue and Rvalue Expression in C#:
But following is not a valid statement and would generate
compile-time error:
10 =20;
C# - Constants and
Literals
C# - Constants and Literals
The constants refer to fixed values that the program may not
alter during its execution. These fixed values also called
literals. Constants can be of any of the basic data types like
an integer constant, a floating constant, a character
constant, or a string literal. There are also enumeration
constant as well.
C# - Constants and Literals
The constants are treated just like regular variables except
that their values cannot be modified after their definition.
C# - Constants and Literals
Integer Literals
An integer literal can be a decimal, or hexadecimal constant.
A prefix specifies the base or radix: 0x or 0X for
hexadecimal, and there is no prefix id for decimal.
An integer literal can also have a suffix that is a combination
of U and L, for unsigned and long, respectively. The suffix
can be uppercase or lowercase and can be in any order.
C# - Constants and Literals
Integer Literals
Here are some examples of integer literals:
212 /*Legal*/
215u /*Legal*/
0xFeeL /*Legal*/
C# - Constants and Literals
Integer Literals
Following are other examples of various types of Integer
literals:
85 /*decimal*/
0x4b /*hexadecimal*/
30 /*int*/
30u /*unsigned int*/
301 /*long*/
30u1 /*unsigned long*/
C# - Constants and Literals
Integer Literals
Following are other examples of various types of Integer
literals:
85 /*decimal*/
0x4b /*hexadecimal*/
30 /*int*/
30u /*unsigned int*/
301 /*long*/
30u1 /*unsigned long*/
C# - Constants and Literals
Floating-point Literals
A floating-point literal has an integer part, a decimal point, a
fractional part, and an exponent part. You can represent
floating point literals either in decimal form or exponential
form.
C# - Constants and Literals
Floating-point Literals
Here are some examples of floating-point literals:
3.14159 /* Legal */
314159E-5F /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */
C# - Constants and Literals
Floating-point Literals
While representing in decimal form, you must include the
decimal point, the exponent, or both; and representing using
exponential form you must include the integer part, the
fractional part, or both. The signed exponent is introduced
by e or E.
C# - Constants and Literals
Character Constants
Character literals are enclosed in single quotes. For example,
'x' and can be stored in a simple variable of char type. A
character literal can be a plain character (such as 'x'), an
escape sequence (such as 't'), or a universal character (such
as 'u02C0').
C# - Constants and Literals
Character Constants
There are certain characters in C# when they are preceded
by a backslash. They have special meaning and they are
used to represent like newline (n) or tab (t). Here, is a list of
some of such escape sequence codes:
C# - Constants and Literals
Character Constants
Demo
C# - Constants and Literals
String Literals
String literals or constants are enclosed in double quotes ""
or with @"". A string contains characters that are similar to
character literals: plain characters, escape sequences, and
universal characters.
You can break a long line into multiple lines using string
literals and separating the parts using whitespaces.
C# - Constants and Literals
String Literals
Here are some examples of string literals. All the three forms
are identical strings.
"hello, dear"
"hello, 
dear"
"hello, " "d" "ear"
@"hello dear"
C# - Constants and Literals
Defining Constants
Constants are defined using const keyword. Syntax for
defining a constant is:
const <data_type> <constant_name> = value;
Demo
Related Content
•TutorialsPoint
www.tutorialspoint.com
Thank You

More Related Content

PDF
Java conditional statements
PPTX
Classes objects in java
PPTX
C# lecture 2: Literals , Variables and Data Types in C#
PPTX
Data types vbnet
PPTX
Constants in java
PPTX
Pointers in c++
PPTX
Data types in php
PDF
Learn C# Programming - Data Types & Type Conversion
Java conditional statements
Classes objects in java
C# lecture 2: Literals , Variables and Data Types in C#
Data types vbnet
Constants in java
Pointers in c++
Data types in php
Learn C# Programming - Data Types & Type Conversion

What's hot (20)

PPT
Methods in C#
PPTX
Variable and constants in Vb.NET
PPT
PHP - Introduction to Object Oriented Programming with PHP
PPTX
Strings in c#
PPTX
Array in c#
PPTX
SUBQUERIES.pptx
PPTX
Type casting
PPT
Generics in java
PPTX
OOPS In JAVA.pptx
PPTX
Data types in java
PPTX
Inheritance in java
PPTX
Operator overloading
PPTX
html-table
PPTX
Pointers in C Programming
PDF
Immutable vs mutable data types in python
PPTX
Regular Expressions 101 Introduction to Regular Expressions
PPTX
C# Strings
PPT
Variables in C Programming
PPT
Function overloading(c++)
PPTX
concept of Array, 1D & 2D array
Methods in C#
Variable and constants in Vb.NET
PHP - Introduction to Object Oriented Programming with PHP
Strings in c#
Array in c#
SUBQUERIES.pptx
Type casting
Generics in java
OOPS In JAVA.pptx
Data types in java
Inheritance in java
Operator overloading
html-table
Pointers in C Programming
Immutable vs mutable data types in python
Regular Expressions 101 Introduction to Regular Expressions
C# Strings
Variables in C Programming
Function overloading(c++)
concept of Array, 1D & 2D array
Ad

Viewers also liked (8)

PPTX
BilgeAdam Nesne Yönelimli Programlama
PDF
Learn C# Programming - Decision Making & Loops
PPT
Donanım Sunusu - 5
PPT
C# Variables and Operators
PPT
C# Sunusu - 1
PPT
Variables
PPT
Variables
PPT
Types of Variables
BilgeAdam Nesne Yönelimli Programlama
Learn C# Programming - Decision Making & Loops
Donanım Sunusu - 5
C# Variables and Operators
C# Sunusu - 1
Variables
Variables
Types of Variables
Ad

Similar to Learn C# Programming - Variables & Constants (20)

DOCX
C programming tutorial
PDF
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
PDF
PSPC--UNIT-2.pdf
PPTX
Data Types, Variables, and Constants in C# Programming
PPTX
INTRODUCTION TO C++.pptx
PPTX
C Program basic concepts using c knoweledge
PPTX
C introduction
PPT
C the basic concepts
DOCX
fds unit1.docx
PPTX
PPTX
Fundamentals of C Programming Language
PPTX
Object Oriented Programming with C++
PPT
history of c.ppt
PPTX
C programming Training in Ambala ! Batra Computer Centre
PPT
U2.ppt
ODP
CProgrammingTutorial
PPTX
programming for problem solving in C and C++.pptx
PPTX
C language ppt
C programming tutorial
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
PSPC--UNIT-2.pdf
Data Types, Variables, and Constants in C# Programming
INTRODUCTION TO C++.pptx
C Program basic concepts using c knoweledge
C introduction
C the basic concepts
fds unit1.docx
Fundamentals of C Programming Language
Object Oriented Programming with C++
history of c.ppt
C programming Training in Ambala ! Batra Computer Centre
U2.ppt
CProgrammingTutorial
programming for problem solving in C and C++.pptx
C language 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)

PDF
cuic standard and advanced reporting.pdf
PPT
Teaching material agriculture food technology
PDF
KodekX | Application Modernization Development
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Spectroscopy.pptx food analysis technology
PPTX
sap open course for s4hana steps from ECC to s4
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
cuic standard and advanced reporting.pdf
Teaching material agriculture food technology
KodekX | Application Modernization Development
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectroscopy.pptx food analysis technology
sap open course for s4hana steps from ECC to s4
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The AUB Centre for AI in Media Proposal.docx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Encapsulation_ Review paper, used for researhc scholars
The Rise and Fall of 3GPP – Time for a Sabbatical?
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Agricultural_Statistics_at_a_Glance_2022_0.pdf

Learn C# Programming - Variables & Constants

  • 1. Learn C# Programming Variables and Constants Eng Teong Cheah Microsoft MVP in Visual Studio & Development Technologies
  • 4. C# - Variables A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable’s memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable.
  • 5. C# - Variables The basic value types provided in C# can be categorized as:
  • 6. C# - Variables C# also allows defining other value types of variable such as enum and reference types of variables such as class, which we will cover in subsequent chapters.
  • 7. C# - Variables Defining Variables Syntax for variable definition in C# is: Here, data_type must be a valid C# data type including char, int, float, double, or any user-defined data type, and variable_list may consist of one or more identifier names separated by commas. <data_type> <variable_list>;
  • 8. C# - Variables Defining Variables Some valid variable definitions are shown here: int i, j, k; char c, ch; float f, salary; double d; You can initialize a variable at the time of definition as: int i = 100;
  • 9. C# - Variables Initializing Variables Variables are initialized (assigned a value) with an equal sign followed by a constant expression. The general form of initialization is: Variables can be initialized in their declaration. The initializer consists of an equal sign followed by a constant expression as: variable_name = value; <data_type> <variable_name> = value;
  • 10. C# - Variables Initializing Variables Some examples are: int d = 3, f = 5; /* initializing d and f. */ byte z = 22; /* initializes z. */ double pi = 3.14159; /*declares an approximation of pi. */ char x = ‘x’;
  • 11. C# - Variables Initializing Variables It is a good programming practice to initialize variables properly, otherwise sometimes program may produce unexpected result.
  • 12. Demo
  • 13. C# - Variables Accepting Values from User The Console class in the System namespace provides function ReadLine() for accepting input from the user and store it into a variable. For example, int num; num = Convert.ToInt32(Console.ReadLine());
  • 14. C# - Variables Accepting Values from User The function Convert.ToInt32() converts the data entered by the user to int data type, because Console.ReadLine() accepts the data in string format.
  • 15. C# - Variables Lvalue and Rvalue Expression in C#: The are two kinds of expressions in C#: - lvalue: An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment. - rvalue: AN expression that is an rvalue may appear on the right-hand but not left-hand side of an assignment.
  • 16. C# - Variables Lvalue and Rvalue Expression in C#: Variables are lvalues and hence they may appear on the left- hand side of an assignment. Numeric literals are rvalues and hence they may not be assigned and cannot appear on the left-hand side. Following is a valid C# statement: int g =20;
  • 17. C# - Variables Lvalue and Rvalue Expression in C#: But following is not a valid statement and would generate compile-time error: 10 =20;
  • 18. C# - Constants and Literals
  • 19. C# - Constants and Literals The constants refer to fixed values that the program may not alter during its execution. These fixed values also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constant as well.
  • 20. C# - Constants and Literals The constants are treated just like regular variables except that their values cannot be modified after their definition.
  • 21. C# - Constants and Literals Integer Literals An integer literal can be a decimal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, and there is no prefix id for decimal. An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.
  • 22. C# - Constants and Literals Integer Literals Here are some examples of integer literals: 212 /*Legal*/ 215u /*Legal*/ 0xFeeL /*Legal*/
  • 23. C# - Constants and Literals Integer Literals Following are other examples of various types of Integer literals: 85 /*decimal*/ 0x4b /*hexadecimal*/ 30 /*int*/ 30u /*unsigned int*/ 301 /*long*/ 30u1 /*unsigned long*/
  • 24. C# - Constants and Literals Integer Literals Following are other examples of various types of Integer literals: 85 /*decimal*/ 0x4b /*hexadecimal*/ 30 /*int*/ 30u /*unsigned int*/ 301 /*long*/ 30u1 /*unsigned long*/
  • 25. C# - Constants and Literals Floating-point Literals A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.
  • 26. C# - Constants and Literals Floating-point Literals Here are some examples of floating-point literals: 3.14159 /* Legal */ 314159E-5F /* Legal */ 510E /* Illegal: incomplete exponent */ 210f /* Illegal: no decimal or exponent */ .e55 /* Illegal: missing integer or fraction */
  • 27. C# - Constants and Literals Floating-point Literals While representing in decimal form, you must include the decimal point, the exponent, or both; and representing using exponential form you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.
  • 28. C# - Constants and Literals Character Constants Character literals are enclosed in single quotes. For example, 'x' and can be stored in a simple variable of char type. A character literal can be a plain character (such as 'x'), an escape sequence (such as 't'), or a universal character (such as 'u02C0').
  • 29. C# - Constants and Literals Character Constants There are certain characters in C# when they are preceded by a backslash. They have special meaning and they are used to represent like newline (n) or tab (t). Here, is a list of some of such escape sequence codes:
  • 30. C# - Constants and Literals Character Constants
  • 31. Demo
  • 32. C# - Constants and Literals String Literals String literals or constants are enclosed in double quotes "" or with @"". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters. You can break a long line into multiple lines using string literals and separating the parts using whitespaces.
  • 33. C# - Constants and Literals String Literals Here are some examples of string literals. All the three forms are identical strings. "hello, dear" "hello, dear" "hello, " "d" "ear" @"hello dear"
  • 34. C# - Constants and Literals Defining Constants Constants are defined using const keyword. Syntax for defining a constant is: const <data_type> <constant_name> = value;
  • 35. Demo