0% found this document useful (0 votes)
122 views

Variables and Data Types in C#

This document discusses variables and data types in C#. It explains that variables store values that can change and are declared with a data type and name. Data types include value types that store actual values and reference types that store memory addresses. The document also covers predefined data types, classification of reference types, naming conventions, comments, and input/output methods.

Uploaded by

Mustafa Adil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Variables and Data Types in C#

This document discusses variables and data types in C#. It explains that variables store values that can change and are declared with a data type and name. Data types include value types that store actual values and reference types that store memory addresses. The document also covers predefined data types, classification of reference types, naming conventions, comments, and input/output methods.

Uploaded by

Mustafa Adil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

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)

You might also like