You are on page 1of 19

CTS

 In .NET “Type” refers to class, interface, structure, enumeration, delegate.


 CTS is a formal specification that documents how types must be defined in order to
be hosted by the CLR
 CTS Class Types
 CTS Interface Types
 CTS Structure Types
 CTS Enumeration Types
 CTS Delegate Types
 CTS Type Members
 Intrinsic CTS Data Types
CTS Class Types
 composed of any number of members (such as constructors, properties, methods, and
events) and data points (fields).
 Syntax:
Class class_name
{
----
----
}
 Eg,
class Calc
{
public int Add(int x, int y)
{
return x + y;
}
}
 Class Characteristic
 Is the class sealed or not?
 Does the class implement any interfaces?
 Is the class abstract or concrete?
 What is the visibility of this class?
CTS Interface Types
 collection of abstract member definitions
 Those abstract member should be implemented by class
 Syntax:
access_mdifier interface interface_name
{
---
---
}
 Eg

public interface IDraw


{
void Draw();
}
CTS Structure Types
 structure can be thought of as a lightweight class
 Eg.,
struct Point
{
// Structures can contain fields.
public int xPos, yPos;
// Structures can contain parameterized constructors.
public Point(int x, int y)
{
xPos = x; yPos = y;
}
// Structures may define methods.
public void PrintPosition()
{
Console.WriteLine("({0}, {1})", xPos, yPos);
}
}
CTS Enumeration Types
 allow you to group name/value pairs
 hold each item in a 32-bit integer
 derive from a common base class, System.Enum
 Syntax:
enum enum_name
{
----
----
}
 Eg.,
enum CharacterType
{
Wizard = 100,
Fighter = 200,
Thief = 300
}
CTS Delegate Types
 Delegate is similar to function pointer in c- langauge
 The key difference is that a .NET delegate is a class that derives from
System.MulticastDelegate
 delegate int BinaryOp(int x, int y);
 provide a way for one object to forward a call to another object
 provide the foundation for the .NET event architecture
 Syntax:
delegate return_type delegate_name(arguments);
 Eg.,
delegate int BinaryOp(int x, int y);
CTS Type Members
 Type Members refer to constructor, finalizer, static constructor, nested type,
operator, method, property, indexer, field, read-only field, constant, event
Intrinsic CTS Data Types
 fundamental data types
 CTS type defined in an assembly named mscorlib.dll
Intrinsic CTS Data Types
CLS
 Different programming language express the same programmatic idioms using different
syntax
Eg.,
C# Language VB Language
// C# method returning ' VB method returning nothing.
nothing. Public Sub MyMethod()
public void MyMethod() ' Some interesting code...
{ End Sub
// Some interesting code...
}

 However the respective compiler should emit similar set of CIL instruction

 The CLS is ultimately a set of rules that compiler builders must conform it
 The only aspects of a type that must conform to the CLS are the member definitions
themselves (i.e., naming conventions, parameters, and return types)

Not conform to CLS Conform to CLS


class Calc class Calc
{ {
// Exposed unsigned data is not CLS public int Add(int x, int y)
compliant! {
public ulong Add(ulong x, ulong y) // As this ulong variable is only used
{ internally,
return x + y; // we are still CLS compliant.
} ulong temp = 0;
} ...
return x + y;
}
}
Ensuring CLS Compliance
 We can ensure the compliance of CLS using .NET attributes
 Eg.,
// Tell the C# compiler to check for CLS compliance.
[assembly: CLSCompliant(true)]
Understanding the Common
Language Runtime
 Runtime environment shared by all languages and platform that are .NET aware.
 CLR is physically represented by mscoree.dll (Common Object Runtime Execution Engine)
 At first CLR resolves the location of assembly and referenced external assembly using
Metadata
 Working process of CLR is shown in below diagram

You might also like