Академический Документы
Профессиональный Документы
Культура Документы
C# 30
C# 3.0
Chapter 3 The Type System
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
.NET
NET Basic Data Types
The CTS pro
provides
ides a rich set of pre
pre-defined
defined
yp
for all .NET languages
g g use
basic types,
This set contains primitive types
System.Int32, System.Double,, System.Decimal
and System.String
Different .NET
NET languages define different
aliases to these .NET basic types
In the next slides you can find the .NET basic types
with their C# alias data type name and the constant
numeric literal if applicable
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
C# .NET
NET type mapping
.NET Type
Description
C# Data Type
Literals/ Cast
System.SByte
sbyte
Cast
System.Byte
byte
Cast
System.Int16
g
integer
g
16-bit signed
short
Cast
System.UInt16
16-bit unsigned
integer
ushort
Cast
System.Int32
int
Default
System UInt32
System.UInt32
32-bit unsigned
integer
uint
Cast
System.Int64
long
L,l
System.UInt64
64-bit unsigned
integer
g
ulong
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
C# .NET
NET type mapping
.NET Type
Description
System.Single
single-precision
(32-bit) floating-point number
float
F,f
System Double
System.Double
A double-precision
(64-bit) floating-point number
double
Default
System.Char
A Unicode character
(a 16-bit character)
char
'A', '\x0041',
(char)65, '\u0041'
bool
true, false
decimal
M,m
System String
System.String
A string of Unicode
characters
string
"s"
s ,@
@"\n"
\n , "\t"
\t
System.Object
object
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Everything is an object
The base of all .NET
NET ttypes
pes is
System.Object
This also includes the .NET primitive types
By defining a common base
base, .NET
NET allows treating all
types in common terms
Such as: defining collection types to store any type of
elements, defining a generic method that can accept any
argument type, etc.
C# 30
System Object
System.Object
Some of the System.Object
S stem Object methods are
virtual
Therefore can be overridden by specific types to
better represent their characteristics
ToString
ToString()
() is a good example
Syste
System.Object
.Object methods
et ods will be p
presented
ese ted later
ate in tthis
s
chapter
The .NET
NET CTS System.Object
System Object type is mapped
in C# to a type called object
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Reference Types
The statement:
Employeee=newEmployee("JoeWise");
Allocates an object
j
space
p
on the heap
p
Assigns it to the required data
Returns a reference to this object, to be stored in the reference
variable
Users
U
can d
define
fi user-defined
d fi d reference
f
types:
t
Classes, interfaces, delegates and events
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
For example:
Th
The statement:
t t
t int
i t i =
=32
32;
32;
allocates a 32-bit space on the stack and moves a 32-bit
value into this space
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Value Types
Being directl
directly manip
manipulated,
lated
Value type objects provide better performance than
reference type objects
Their functionality,
y, however,, is limited
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
B
Based
d on thi
this kknowledge,
l d
ttake
k a llook
k att
the code shown in the following
g slides and
guess what the programs output would be
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
C# 30
C# 30
Intermediate Thoughts
Value types have a simple
simple, efficient form
form,
in which they can be handled efficiently
But all types derive from
System.Object, so they can be treated in
System.Object
common terms
This means that a value
al e ttype
pe variable
ariable can be passed
to a method that gets an object as a parameter
This should also mean that the following statements
are valid:
int i =42;
objecto=i;//???
objecto i;//???
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Boxing
Ha
Having
ing ssuch
ch different forms
forms, ho
how can a
yp variable be assigned
g
to a
value-type
reference type?
Lets
Let s explore the MSIL code generated from the two
statements above:
.locals([0]int32i,
l
l ([0] i t32 i
[1]objecto)
IL 0000:ldc i4 s42
IL_0000:ldc.i4.s42
IL_0002:stloc.0
IL_0003:ldloc.0
IL_0004:box[mscorlib]System.Int32
IL_0009:stloc.1
IL_000a:ret
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Boxing
int
i
t i =42;
42
object o=i;
Heap
Stack
System.Int32
42
42 (i)
0xdddd (o)
C# 30
Unboxing
Can we retrieve a boxed value type?
int i =42;
.locals([0]int32i,
locals([0]int32i
objecto=i;
[1]objecto,
[2]int32j)
[
]
j)
int j
j=(int)o;//???
(int)o;//???
IL_0000:ldc.i4.s42
IL_0002:stloc.0
IL 0003:ldloc 0
IL_0003:ldloc.0
IL_0004:box[mscorlib]System.Int32
IL_0009:stloc.1
_
IL_000a:ldloc.1
IL_000b: unbox [mscorlib]System.Int32
IL
0010:ldind i4
IL_0010:ldind.i4
IL_0011:stloc.2
IL_0012:ret
_
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
System.Object
System Object Class Members
Following are the Object class methods
methods,
that any type in .Net ultimately has:
publicvirtualbool Equals(objecto);
publicstaticbool Equals(objecto1,objecto2);
Equals(objecto1 objecto2);
publicstaticbool ReferenceEquals(objecto1,
objecto2);
publicvirtualint GetHashCode();
publicTypeGetType();
publicvirtualstringToString();
~Object();//(Object.Finalize())
protectedobjectMemberwiseClone();
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
publicstaticbool Equals(
objecto1,objecto2);
Calls o1.Equals(o2)
publicstaticbool ReferenceEquals(
objecto1,objecto2);
t
i
h th th
ifi d objects
bj t refer
f tto th
D
Determines
whether
the specified
the same
instance
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Comparing Objects
Class Exercise
Write next to each output operation
whether the output will be True (T) or
F
l (F)
False
(F):
usingSystem;
classCl {}
classCompareApp
{
staticvoidMain()
{
Cl cl1=newCl();
Cl cl2=newCl();
cl2 newCl();
int i =5;
int j=5;
objecto=i;
bj
i
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Comparing Objects
Class Exercise
Console.WriteLine("referenceType:");
Console.WriteLine(
"cl1==cl2:{0}",cl1==cl2);
Console.WriteLine(
"cl1.Equals(cl2):{0}",
cl1.Equals(cl2));
Console.WriteLine(
"Equals(cl1,cl2):{0}",
Equals(cl1,cl2));
l ( l
l ))
Console.WriteLine(
"R f
"ReferenceEquals(cl1,cl2):{0}",
E
l ( l1 l2) {0}"
ReferenceEquals(cl1,cl2));
______
______
______
______
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Comparing Objects
Class Exercise
Console.WriteLine("ValueType:");
Console.WriteLine(
"i ==j:{0}",i ==j);
Console.WriteLine(
"i.Equals(j):{0}",i.Equals(j));
Console.WriteLine(
"Equals(i,j):{0}",Equals(i,j));
Console.WriteLine(
"ReferenceEquals(i,j):{0}",
ReferenceEquals(i,j));
______
______
______
______
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Comparing Objects
Class Exercise
Console.WriteLine(
Console
WriteLine("misc:");
misc: );
Console.WriteLine(
"ReferenceEquals(cl1,cl1):{0}",
ReferenceEquals(cl1,cl1):{0} ,
ReferenceEquals(cl1,cl1));
______
Co so e.
Console.WriteLine(
te
e(
"ReferenceEquals(i,j):{0}",
q
( , j));
______
ReferenceEquals(i,j));
Console.WriteLine(
"ReferenceEquals(i,i):{0}",
ReferenceEquals(i,i));
______
Console.WriteLine("ReferenceEquals(o,o):{0}",
ReferenceEquals(o,o));
______
}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
p
publicType
publicTypeGetType
yp GetType();
yp ();
();
Returns a Type-derived object that represents the
exact runtime type of this object
The returned Type object can be used to retrieve
type related information
Will be later discussed in the Reflection context
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
For
F user-defined
d fi d ttypes
it is recommended to override this method to return a
meaningful string representing the objects state
Good for debugging purposes too (the debugger
displays this in the Watch window and elsewhere)
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
protectedobjectMemberwiseClone();
p
j
();
Perform a shallow copy on an object
To implement a deep copy,
copy a type should implement
the ICloneable interface (Clone()
Clone() method)
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Type Conversion
The CTS defines con
conversions
ersions for basic
yp
types
It also provides means to define conversions for userdefined types
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
The System.Convert
System Convert Class
This class defines many ToXXX static
methods
E.g., ToInt32(string), ToBoolean(int), etc.
Letting us convert base data types to other base data
types
In
I case off loss
l
off precision
i i
No exception will be thrown
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Converting Strings
Reading an integer from the Console
g Convert:
using
i
int
i =Convert.ToInt
=Convert.ToInt32
32(
(Console.ReadLine
Console.ReadLine());
i ());
())
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
Chapter Summary
CTS is the foundation of the type
yp system
y
The core for the CLR cross-language support
.NET
NET basic types and their C# aliases
In .NET, everything
y
g is an object
j
System.Object is the base of all types
Types
T
are divided
di id d iinto
t ttwo categories:
t
i
Reference types and value types
Boxing and unboxing bridge the gap