Hand Out 1
Hand Out 1
C# Version History
C# was first introduced with .NET Framework 1.0 in the year 2002 and
evolved much since then. The following table lists important features
introduced in each version of C#:
C# 1.0 .NET Framework 1.0/1.1 Visual Studio .NET 2002 Basic features
C# 2.0 .NET Framework 2.0 Visual Studio 2005 Generics Partial types Anonymous methods Iterators
Nullable types Private setters (properties) Method
group conversions (delegates) Covariance and Contra-
variance Static classes
C# 3.0 .NET Framework 3.0\3.5 Visual Studio 2008 Implicitly typed local variables Object and collection
initializers Auto-Implemented properties Anonymous
types Extension methods Query expressions Lambda
expressions Expression trees Partial Methods
C# 4.0 .NET Framework 4.0 Visual Studio 2010 Dynamic binding (late binding) Named and optional
arguments Generic co- and contravariance Embedded
interop types
C# 5.0 .NET Framework 4.5 Visual Studio 2012/2013 Async features Caller information
C# 6.0 .NET Framework 4.6 Visual Studio 2013/2015 Expression Bodied Methods Auto-property initializer
Name of Expression Primary constructor Await in
catch block Exception Filter String Interpolation
C# 7.0 .NET Core 2.0 Visual Studio 2017 out variables Tuples Discards Pattern Matching Local
functions Generalized async return types more..
C# 8.0 .NET Core 3.0 Visual Studio 2019 Read only members Default interface methods Using
declarations Static local functions Disposable ref
structs
Nullable reference types more..
C# - Data Types
1/11
C# is a strongly-typed language. It means we must declare the
type of a variable that indicates the kind of values it is going to store, such
as integer, float, decimal, text, etc.
C# mainly categorized data types in two types: Value types and Reference
types. Value types include simple types (such as int, float, bool, and char),
enum types, struct types, and Nullable value types. Reference types
include class types, interface types, delegate types, and array types. Learn
about value types and reference types in detail in the next chapter.
2/11
Suffi
Type Description Range x
byte 8-bit unsigned integer 0 to 255
sbyte 8-bit signed integer -128 to 127
short 16-bit signed integer -32,768 to 32,767
ushort 16-bit unsigned integer 0 to 65,535
int 32-bit signed integer -2,147,483,648
to
2,147,483,647
uint 32-bit unsigned integer 0 to 4,294,967,295 u
long 64-bit signed integer -9,223,372,036,854,775,808 l
to
9,223,372,036,854,775,807
ulong 64-bit unsigned integer 0 to 18,446,744,073,709,551,615 ul
float 32-bit Single-precision -3.402823e38 to 3.402823e38 f
floating point type
double 64-bit double-precision -1.79769313486232e308 to 1.79769313486232e308 d
floating point type
decimal 128-bit decimal type for (+ or -)1.0 x 10e-28 to 7.9 x 10e28 m
financial and monetary
calculations
char 16-bit single Unicode Any valid character, e.g. a,*, \x0058 (hex), or\u0058
character (Unicode)
bool 8-bit logical true/false True or False
value
object Base type of all other
types.
string A sequence of Unicode
characters
DateTim Represents date and 0:00:00am 1/1/01
e time to
11:59:59pm 12/31/9999
As you can see in the above table that each data type (except string and
object) includes value range. The compiler will give an error if the value
goes out of datatype's permitted range. For example, int data type's range
is -2,147,483,648 to 2,147,483,647. So if you assign a value which is not in
this range, then the compiler would give an error.
3/11
The value of unsigned integers, long, float, double, and decimal type must
be suffix by u,l,f,d, and m, respectively.
It means that whether you define a variable of int or Int32, both are the
same.
int i = 345;
4/11
Int32 i = 345;// same as above
Default Values
Every data type has a default value. Numeric type is 0, boolean has false,
and char has '\0' as default value. Use the default(typename) to assign
a default value of the data type or C# 7.1 onward, use default literal.
int i = default(int); // 0
float f = default(float);// 0
decimal d = default(decimal);// 0
bool b = default(bool);// false
char c = default(char);// '\0'
// C# 7.1 onwards
int i = default; // 0
float f = default;// 0
decimal d = default;// 0
bool b = default;// false
char c = default;// '\0'
Conversions
The values of certain data types are automatically converted to different
data types in C#. This is called an implicit conversion.
Implicit Conversion
From To
sbyte short, int, long, float, double, decimal
5/11
Implicit Conversion
From To
byte short, ushort, int, uint, long, ulong, float, double, decimal
short int, long, float, double, or decimal
ushort int, uint, long, ulong, float, double, or decimal
int long, float, double, or decimal.
uint long, ulong, float, double, or decimal
long float, double, or decimal
ulong float, double, or decimal
char ushort, int, uint, long, ulong, float, double, or decimal
float Double
However, not all data types are implicitly converted to other data types. For
example, int type cannot be converted to uint implicitly. It must be
specified explicitly, as shown below.
Numbers in C#
Numbers, in general, can be divided into two types: Integer type and
floating-point types.
Integer type numbers are whole numbers without decimal points. It can
be negative or positive numbers.
C# includes different data types for integer types and floating-point types
based on their size in the memory and capacity to store numbers.
6/11
The following figure illustrates numeric types in C#.
Numeric Types
Integer Types
Integer type numbers are positive or negative whole numbers without
decimal points. C# includes four data types for integer numbers: byte,
short, int, and long.
Byte
The byte data type stores numbers from 0 to 255. It occupies 8-bit in the
memory. The byte keyword is an alias of the Byte struct in .NET.
The sbyte is the same as byte, but it can store negative numbers from -128
to 127. The sbyte keyword is an alias for SByte struct in .NET.
Console.WriteLine(Byte.MaxValue);//255
Console.WriteLine(Byte.MinValue);//0
Console.WriteLine(SByte.MaxValue);//127
Console.WriteLine(SByte.MinValue);//-128
7/11
Short
The short data type is a signed integer that can store numbers from -
32,768 to 32,767. It occupies 16-bit memory. The short keyword is an alias
for Int16 struct in .NET.
The ushort data type is an unsigned integer. It can store only positive
numbers from 0 to 65,535. The ushort keyword is an alias for UInt16 struct
in .NET.
Console.WriteLine(Int16.MaxValue);//32767
Console.WriteLine(Int16.MinValue);//-32768
Console.WriteLine(UInt16.MaxValue);//65535
Console.WriteLine(UInt16.MinValue);//0
Int
The int data type is 32-bit signed integer. It can store numbers from -
2,147,483,648 to 2,147,483,647. The int keyword is an alias of Int32 struct
in .NET.
8/11
Console.WriteLine(Int32.MaxValue);//2147483647
Console.WriteLine(Int32.MinValue);//-2147483648
Console.WriteLine(UInt32.MaxValue);//4294967295
Console.WriteLine(UInt32.MinValue);//0
The int data type is also used for hexadecimal and binary numbers. A
hexadecimal number starts with 0x or 0X prefix. C# 7.2 onwards, a binary
number starts with 0b or 0B.
Console.WriteLine(hex);
Console.WriteLine(binary);
Long
The long type is 64-bit signed integers. It can store numbers from -
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use l or L suffix
with number to assign it to long type variable. The long keyword is an alias
of Int64 struct in .NET.
Console.WriteLine(Int64.MaxValue);//9223372036854775807
Console.WriteLine(Int64.MinValue);//-9223372036854775808
Console.WriteLine(UInt64.MaxValue);//18446744073709551615
Console.WriteLine(UInt64.MinValue);//0
Float
The float data type can store fractional numbers from 3.4e−038 to
3.4e+038. It occupies 4 bytes in the memory. The float keyword is an alias
of Single struct in .NET.
Example: float
float f1 = 123456.5F;
float f2 = 1.123456f;
Console.WriteLine(f1);//123456.5
Console.WriteLine(f2);//1.123456
Double
The double data type can store fractional numbers from 1.7e−308 to
1.7e+308. It occupies 8 bytes in the memory. The double keyword is an
alias of the Double struct in .NET.
Example: double
double d1 = 12345678912345.5d;
double d2 = 1.123456789123456d;
Console.WriteLine(d1);//12345678912345.5
Console.WriteLine(d2);//1.123456789123456
Decimal
The decimal data type can store fractional numbers from ±1.0 x 10-28 to
±7.9228 x 1028. It occupies 16 bytes in the memory. The decimal is a
keyword alias of the Decimal struct in .NET.
The decimal type has more precision and a smaller range than both float
and double, and so it is appropriate for financial and monetary calculations.
10/11
Example: decimal
decimal d1 = 123456789123456789123456789.5m;
decimal d2 = 1.1234567891345679123456789123m;
Console.WriteLine(d1);
Console.WriteLine(d2);
Scientific Notation
Use e or E to indicate the power of 10 as exponent part of
scientific notation with float, double or decimal.
Example:
double d = 0.12e2;
Console.WriteLine(d); // 12;
float f = 123.45e-2f;
Console.WriteLine(f); // 1.2345
decimal m = 1.2e6m;
Console.WriteLine(m);// 1200000
11/11