Variables and Data Types in C#
Variables and Data Types in C#
Lecture 2
Variables and Data Types in C#
• A variable is an entity identified by a unique
whose value can keep changing.
• The following syntax is used to declare
variables in C#
<datatype> <variableName>;
<variableName> = value;
Data Types
• You can store different types of values such as
numbers, characters or string in different variables.
• Data types are divided into two categories. These
are:
• Value Types: Variables of value types store actual
values.
• Reference Types: Variables of reference type
store the memory address of other variables in a
heap
Predefined Data Types
• The Predefined Data Types are referred to as
basic data types in a C#.
Classification
• Reference data types store the memory
address of other variables
• Reference types can be classified as:
• Object: is a base class for all predefined and
user-defined data types.
• String: Unicode character string value
• Class: is a user-defined structure that
contains variables and methods.
• Delegate: is a user-defined reference type
that stores the reference of one or more
method.
• Interface: is a type of use-defined class is
used for multiple inheritance.
• Array : is a user-defined structure that
contains values of the same data types.
Rules
• A variables name can contains letters, digits,
and underscore characters.
• The first character must b ea letter not digit.
• C# is a case-sensitive language.
• C# keywords cannot be used as variables
names.
Comments
• In C#, comments are given by programmer to
provide information about a piece of code.
• C# supports two types of comments:
• Single-line Comments: begins with two
forward slashes(//)
• Multi-line Comments: begins with a forward
slash followed by an asterisk (/*) and end
with an asterisk followed by forward slash
Input and Output
• Console Operations :
Console operation are tasks performed on
the command line interface using executable
commands.
• All Console application consist of three
streams:
• Standard in stream takes the input passes it to
the console application for processing.
• Standard out stream displays the output on
the monitor .
• Standard err stream displays error message on
the monitor
Output methods
• In C#, all console operations are handled by the
Console class of System namespace.
• To write data on the console you need the
standard output stream. This stream is provided
by the output methods of Console Class.
• They are:
• Console.Write() – write any types of data.
• Console.WriteLine() _ Writes any type of data
and ends with a new line.
cont
• Code syntax:
Console.Write(“<data>” + variables);
• Example:
Console.Write(“Hello Word);
Inputs Methods
• To read data you need the standard input
stream is provided by the input methods of
the console class.
Console.Read() – read a single character
Console.ReadLine() – reads a line of strings
cont
• Example:
string name;
Console.WriteLine(“Enter your name: ”);
name= Console.ReadLine ();
Console.WriteLine(“You are {0}”, name)