0% found this document useful (0 votes)
14 views11 pages

Hand Out 1

The document outlines the history of C# from its introduction in 2002 to the latest features in C# 8.0, detailing the important features introduced in each version and the data types used in C#. It explains the distinction between value types and reference types, provides examples of predefined data types, and discusses default values and conversions. Additionally, it covers numeric types, including integer and floating-point types, along with their respective ranges and usage in C#.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

Hand Out 1

The document outlines the history of C# from its introduction in 2002 to the latest features in C# 8.0, detailing the important features introduced in each version and the data types used in C#. It explains the distinction between value types and reference types, provides examples of predefined data types, and discusses default values and conversions. Additionally, it covers numeric types, including integer and floating-point types, along with their respective ranges and usage in C#.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

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#:

Version .NET Framework Visual Studio Important Features

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.

The following declares and initialized variables of different data types.

Example: Variables of Different Data Types


string stringVar = "Hello World!!";
int intVar = 100;
float floatVar = 10.2f;
char charVar = 'A';
bool boolVar = true;

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.

Predefined Data Types in C#


C# includes some predefined value types and reference types. The
following table lists predefined data types:

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.

Example: Compile time error


// compile time error: Cannot implicitly convert type 'long' to
'int'.
int i = 21474836470;

3/11
The value of unsigned integers, long, float, double, and decimal type must
be suffix by u,l,f,d, and m, respectively.

Example: Value Suffix


uint ui = 100u;
float fl = 10.2f;
long l = 45755452222222l;
ulong ul = 45755452222222ul;
double d = 11452222.555d;
decimal mon = 1000.15m;
Try it

Alias vs .NET Type


The predefined data types are alias to their .NET type (CLR class) name.
The following table lists alias for predefined data types and related .NET
class name.

Alias .NET Type Type


byte System.Byte struct
sbyte System.SByte struct
int System.Int32 struct
uint System.UInt32 struct
short System.Int16 struct
ushort System.UInt16 struct
long System.Int64 struct
ulong System.UInt64 struct
float System.Single struct
double System.Double struct
char System.Char struct
bool System.Boolean struct
object System.Object Class
string System.String Class
decimal System.Decimal struct
DateTim System.DateTime struct
e

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.

Example: Implicit Conversion


int i = 345;
float f = i;

Console.WriteLine(f); //output: 345

In the above example, the value of an integer variable i is assigned to the


variable of float type f because this conversion operation is predefined in
C#.

The following is an implicit data type conversion table.

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

Conversions from int, uint, long, or ulong to float and


from long or ulong to double may cause a loss of precision. No data type
implicitly converted to the char type.

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.

Example: Explicit Conversion


public static void Main()
{
int i = 100;
uint u = (uint) i;
Console.Write(i);
}

In the above example, integer i is converted to uint explicitly by specifying


uint in the brackets (uint). This will convert an integer to uint.

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.

Floating-point type is numbers with one or more 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.

Example: byte, sbyte


byte b1 = 255;
byte b2 = -128;// compile-time error: Constant value '-128'
cannot be converted to a 'byte'
sbyte sb1 = -128;
sbyte sb2 = 127;

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.

Example: short, ushort


short s1 = -32768;
short s2 = 32767;
short s3 = 35000;//Compile-time error: Constant value
'35000' cannot be converted to a 'short'

ushort us1 = 65535;


ushort us2 = -32000; //Compile-time error: Constant value
'-32000' cannot be converted to a 'ushort'

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.

The uint is 32-bit unsigned integer. The uint keyword is an alias


of UInt32 struct in .NET. It can store positive numbers from 0 to
4,294,967,295. Optionally use U or u suffix after a number to assign it to
uint variable.

Example: int, uint


int i = -2147483648;
int j = 2147483647;
int k = 4294967295; //Compile-time error: Cannot implicitly
convert type 'uint' to 'int'.

uint ui1 = 4294967295;


uint ui2 =-1; //Compile-time error: Constant value '-1'
cannot be converted to a 'uint'

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.

Example: Hexadecimal, Binary


int hex = 0x2F;
int binary = 0b_0010_1111;

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.

The ulong type stores positive numbers from 0 to


18,446,744,073,709,551,615. If a number is suffixed by UL, Ul, uL, ul, LU,
Lu, lU, or lu, its type is ulong. The uint keyword is an alias of UInt64 struct
in .NET.

Example: long, ulong


long l1 = -9223372036854775808;
long l2 = 9223372036854775807;

ulong ul1 = 18223372036854775808ul;


ulong ul2 = 18223372036854775808UL;

Console.WriteLine(Int64.MaxValue);//9223372036854775807
Console.WriteLine(Int64.MinValue);//-9223372036854775808
Console.WriteLine(UInt64.MaxValue);//18446744073709551615
Console.WriteLine(UInt64.MinValue);//0

Floating Point Types


9/11
Floating-point numbers are positive or negative numbers with one or more
decimal points. C# includes three data types for floating-point numbers:
float, double, and decimal.

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.

Use f or F suffix with literal to make it float type.

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.

Use d or D suffix with literal to make it double type.

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.

Use m or M suffix with literal to make it decimal type.

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

You might also like