Fundamentals of C#
Learn commonly used data types and operators in C#.
Data types in C#
Every variable in C# needs to be declared with the help of a data type. It indicates whether a variable can store only numbers or can also store other symbols as its value.
Common data types
The following are the commonly used data types in C#:
int
: Integer values (5
,0
,-1
, etc.)double
: Floating-point values (2.33333
,-2500.001
,20.0
, etc.)bool
: Boolean values (true
,false
).char
: Character values (a
,0
,$
etc)string
: String values (educative
,C#
, etc.)
Variable declaration
The C# language requires the programmer to indicate the data type of the variables used in a program. The following code demonstrates the declaration of variables to input their values with suitable prompts and to display them with suitable labels:
class Test { static void Main() { string str; // Declaring a variable of type string int number; // Declaring a variable of type integer double real; // Declaring a variable of type double bool isTrue; // Declaring a variable of type bool char letter; // Declaring a variable of type char System.Console.WriteLine("Please input a word: "); str = System.Console.ReadLine(); System.Console.WriteLine("Please input an integer: "); number = int.Parse(System.Console.ReadLine()); System.Console.WriteLine("Please input a real number: "); real = double.Parse(System.Console.ReadLine()); System.Console.WriteLine("Please input a boolean value true or false: "); isTrue = bool.Parse(System.Console.ReadLine()); System.Console.WriteLine("Please input any character: "); letter = char.Parse(System.Console.ReadLine()); System.Console.WriteLine("The value of each variable is enclosed in brackets: "); System.Console.WriteLine("str (" + str + ")"); System.Console.WriteLine("number (" + number + ")"); System.Console.WriteLine("fraction (" + real + ")"); System.Console.WriteLine("isTrue (" + isTrue + ")"); System.Console.WriteLine("letter (" + letter + ")"); } }
Variable declaration
Explanation
- Lines 5–9: In these lines, the variables are declared so that they can be used to store values input by the user. There are different types of variables that are declared;
str
is a variable of thestring
type.number
is a variable of theint
type.real
is a variable of thedouble
type.isTrue
is a variable of thebool
type.letter
is a variable of thechar
type.
- Lines 11–20: These lines output a prompt using
System.Console.WriteLine()
, demand input from the user usingSystem.Console.ReadLine()
, and then store them in the relevant variable. - Lines 22–27: These lines output the variable name as a label and the value enclosed in a bracket.
The following code demonstrates the declaration of the variables without inputting their values from the user:
Press + to interact
class Test{static void Main(){string str; // Declaring a variable of type stringint number; // Declaring a variable of type integerdouble real; // Declaring a variable of type doublebool isTrue; // Declaring a variable of type boolSystem.Console.WriteLine("The value of each variable is enclosed in brackets: ");System.Console.WriteLine("str (" + str + ")");System.Console.WriteLine("number (" + number + ")");System.Console.WriteLine("fraction (" + real + ")");System.Console.WriteLine("isTrue (" + isTrue + ")");}}
In the above code, lines 5–8 declare the variables str
, number
, real
, and isTrue
...
Access this course and 1400+ top-rated courses and projects.