C# FAQ 7: What Are Data Types, Variables And Operators

Data Types and Variables are the core part of the C# language since it represents the various ways to express numbers, characters, and strings. As an example, if you would like to add two numbers, you will have to write code as shown below
  1. int X = 200;  
  2. int Y = 400;  
  3. int Z = X+Y;  
  4. Console.WriteLine(Z);  
Even though the above code is not a complete C# code listing, it shows the correct way to declare values. While int represents the built-in data type called Integer, the characters X, Y and Z in the above code are termed as variables, which are used to store data. You cannot perform any work without storing data. Hence, variables form part and parcel of C# programming language.
 
As you can see in the above code, 200 and 400 are values and the result is stored in another variable Z. However, you should note that each data type have a fixed range beyond which you cannot store values. This means you cannot store a large number using integer data type and vice versa. As a programmer, you should select an appropriate data type before beginning to code and store values relevant to them.
 
If you store incorrect values using any data type, the program will display errors (CS0266) during compilation stage. Look at the same source code given below
  1. using System;  
  2. class Add {  
  3. public static void Main() {  
  4. int X = 2000;  
  5. int Y = 4000;  
  6. int Z = X+Y;  
  7. Console.WriteLine ("The answer is " +Z);  
  8. }  
  9. }  
The above code will not display any compilation errors since we have stored correct values. However, if you attempt to compile the code given below, you will be able to view the compilation error.
  1. using System;  
  2. class Add {  
  3. public static void Main() {  
  4. int X = 200000000000000000;  
  5. int Y = 400000000000000000;  
  6. int Z = X+Y;  
  7. Console.WriteLine ("The answer is " +Z);  
  8. }  
  9. }  
If you compile the above code, you will view the following error message,

Different Types of Data Types

The .NET Framework provides several data types which you can use for developing applications. The data types as shown in the below table are provided by the Common Language Runtime (CLR). Hence all .NET Framework languages including C# can take advantage of them.
 

Data Type Prefix

.NET Data Type

Min Value

Max Value

sbyte

System.Sbyte

-128

127

byte

System.Byte

0

255

short

System.Int16

-32,768

32,767

ushort

System.UInt16

0

65,535

int

System.Int32

-2,147,483,648

2,147,483,647

uint

System.UInt32

0

4,294,967,295

long

System.Int64

-9,223372,036,854,775,808

9,223372,036,854,775,808

ulong

System.UInt64

0

18,446,744,073,709,551,615

char

System.Char

0

65,535

float

System.Single

1.5 x 10-45

3.4 x 10 38

double

System.Double

5.0 x 10-324

1.7 x 1010308

bool

System.Boolean

False (0)

True (1)

decimal

System.Decimal

1.0 x 10-28

7.9 x 1028

Operators

Operators enable you to manipulate values and perform arithmetical operations. For example, if you need to multiply two numbers, you need to make use of * symbol. Likewise, for addition, you should use + symbol.
 
In programming terminology, these symbols are called as Operators, which you will use during the development of applications which largely uses mathematical functions like Calculator. In the below table, you will find a wide range of operators in C# in the order of their precedence.
 

Name of the Operator

Description

Primary Operators

() . [] x++ x-- new typeof sizeof checked unchecked

Unary

+ - ! - ++x --x

Multiplicative

* / %

Additive

+ -

Shift

<< >>

Relational

< > <= >= is

Equality

= = !=

Logical AND

&

Logical XOR

^

Logical OR

|

Conditional AND

&& (Used for evaluating conditions using if-else)

Conditional OR

|| (Used for evaluating conditions using if-else)

Conditional

?: (Used for evaluating conditions instead of if-else)

Assignment

= *= /= %= += -= <<= >>= &= ^= !=

Read more articles on C#:

Up Next
    Ebook Download
    View all
    Learn
    View all