SlideShare a Scribd company logo
1 of 28
Free Ebooks Download
Mba Ebooks
By Edhole
Mba ebooks
Free ebooks download
http://ebooks.edhole.co
3. Data Types
http://ebooks.edhole.com
3Microsoft
Objectives
“.NET is designed around the CTS, or Common Type System. The
CTS is what allows assemblies, written in different languages, to
work together. To ensure interoperability across languages,
Microsoft has also defined the CLS, or Common Language
Specification, a subset of the CTS that all languages support.
Otherwise, the types in C# are what you would expect from a
modern OOPL…”
• The Common Type System
• Value vs. reference types
• Arrays
• Namespaces
http://ebooks.edhole.com
4Microsoft
Part 1
• The Common Type System…
http://ebooks.edhole.com
5Microsoft
The Common Type System (CTS)
• CTS is based on a hierarchy of classes defined in FCL
– all types inherit from Object (all except interface types)
String Array ValueType Exception Delegate Class1
Multicast
Delegate
Class2
Class3
Object
Enum1
Structure1Enum
Primitive types
Boolean
Byte
Int16
Int32
Int64
Char
Single
Double
Decimal
DateTime
System-defined types
User-defined types
Delegate1
TimeSpan
Guid
http://ebooks.edhole.com
6Microsoft
The Common Language Specification (CLS)
• Not all languages support all CTS types and features
– C# supports unsigned integer types, VB.NET does not
– C# is case sensitive, VB.NET is not
– C# supports pointer types (in unsafe mode), VB.NET does not
– C# supports operator overloading, VB.NET does not
• CLS was drafted to promote language interoperability
– vast majority of classes within FCL are CLS-compliant
http://ebooks.edhole.com
7Microsoft
Mapping C# to CTS
• Language keywords map to common CTS classes:
Keyword Description Special format for literals
bool Boolean true false
char 16 bit Unicode character 'A' 'x0041' 'u0041'
sbyte 8 bit signed integer none
byte 8 bit unsigned integer none
short 16 bit signed integer none
ushort 16 bit unsigned integer none
int 32 bit signed integer none
uint 32 bit unsigned integer U suffix
long 64 bit signed integer L or l suffix
ulong 64 bit unsigned integer U/u and L/l suffix
float 32 bit floating point F or f suffix
double 64 bit floating point no suffix
decimal 128 bit high precision M or m suffix
string character sequence "hello", @"C:dirfile.txt"
http://ebooks.edhole.com
8Microsoft
Example
• An example of using types in C#
– declare before you use (compiler enforced)
– initialize before you use (compiler enforced)
public class App
{
public static void Main()
{
int width, height;
width = 2;
height = 4;
int area = width * height;
int x;
int y = x * 2;
...
}
}
declarations
decl + initializer
error, x not set
http://ebooks.edhole.com
9Microsoft
Type conversion
• Some automatic type conversions available
– from smaller to larger types
• Otherwise you need a cast or an explicit conversion…
– typecast syntax is type name inside parentheses
– conversion based on System.Convert class
int i = 5;
double d = 3.2;
string s = "496";
d = i;
i = (int) d;
i = System.Convert.ToInt32(s);
implicit conversion
typecast required
conversion required
http://ebooks.edhole.com
10Microsoft
Part 2
• Value vs. reference types…
http://ebooks.edhole.com
11Microsoft
Value vs. reference types
• C# separates data types into two categories
• Value types:
– variable represents a value ("bits")
• Reference types:
– variable represents a reference to a heap-based object
– actual data resides in the object
int i;
i = 10;
10
string s;
s = "calico";
"calico"
http://ebooks.edhole.com
12Microsoft
How do you know which types are which?
• Memorization!
• Though it's pretty obvious based on past experience
– primitive types like bool, int and double are values
– remainder are reference types
int i;
string s;
Customer c1, c2;
i = 23;
s = "a message";
c1 = null;
c2 = new Customer(…);
http://ebooks.edhole.com
13Microsoft
Boxing and Unboxing
• When necessary, C# will auto-convert value <==> object
– value ==> object is called "boxing"
– object ==> value is called "unboxing"
int i, j;
object obj;
string s;
i = 32;
obj = i; // boxed copy!
i = 19;
j = (int) obj; // unboxed!
s = j.ToString(); // boxed!
s = 99.ToString(); // boxed!
http://ebooks.edhole.com
14Microsoft
User-defined reference types
• Classes!
– for example, Customer class we worked with earlier…
public class Customer
{
public string Name; // fields
public int ID;
public Customer(string name, int id) // constructor
{
this.Name = name;
this.ID = id;
}
public override string ToString() // method
{ return "Customer: " + this.Name; }
}
http://ebooks.edhole.com
15Microsoft
Working with reference types…
• Creating, assigning, and comparing:
Customer c1, c2, c3;
string s1, s2;
c1 = new Customer("joe hummel", 36259);
c2 = new Customer("marybeth lore", 55298);
c3 = null; // c3 references no object
c3 = c1; // c3 now references same obj as c1
if (c1 == null) ... // do I ref an object?
if (c1 == c2) ... // compares references
if (c1.Equals(c2)) ... // compares objects
if (s1 == s2) ... // exception: == overloaded to
// compare string data
http://ebooks.edhole.com
16Microsoft
Defining equality
• Classes should override Equals
public class Customer
{
.
.
.
public override bool Equals(object obj)
{
Customer other;
if ((obj == null) || (!(obj is Customer)))
return false; // definitely not equal
other = (Customer) obj; // typecast to access
return this.ID == other.ID; // equal if same id...
}
http://ebooks.edhole.com
17Microsoft
GetHashCode
• If you override Equals, must also override GetHashCode:
public class Customer
{
.
.
.
public override int GetHashCode()
{
return this.id.GetHashCode();
}
http://ebooks.edhole.com
18Microsoft
Part 3
• Arrays…
http://ebooks.edhole.com
19Microsoft
Arrays
• Arrays are reference types
– based on Array class in FCL
– must be created using new
– 0-based indexing
– assigned default values (0 for numeric, null for references,
etc.)
int[] a;
a = new int[5];
a[0] = 17;
a[1] = 32;
int x = a[0] + a[1] + a[4];
int l = a.Length;
element access
create
number of elements
http://ebooks.edhole.com
20Microsoft
Multi-dimensional arrays
• C# supports arrays as a single object OR array of arrays
– latter allows you to implement jagged arrays
Customer[,] twoD;
int[][] jagged2D;
// 2D array as single object
twoD = new Customer[10, 100];
twoD[0, 0] = new Customer(…);
twoD[9, 99] = new Customer(…);
// 2D array as array of arrays
jagged2D = new int[10][];
jagged2D[0] = new int[10];
jagged2D[1] = new int[20];
jagged2D[9] = new int[100];
jagged2D[0][0] = 1;
jagged2D[9][99] = 100;
http://ebooks.edhole.com
21Microsoft
Part 4
• Namespaces…
http://ebooks.edhole.com
22Microsoft
Namespaces
• Namespaces are a means for organizing types
– a namespace N is a set of names scoped by N
– namespaces are often nested
namespace Workshop
{
public class Customer
{
.
.
.
}
public class Product
{
.
.
.
}
}//namespace
Workshop.Customer
http://ebooks.edhole.com
23Microsoft
Example
• Framework Class Library (FCL)
contains 1000's of classes
– how to organize?
– how to avoid name collisions?
• with FCL
• within FCL
http://ebooks.edhole.com
24Microsoft
FCL namespaces
• FCL's outermost namespace is "System"
• FCL technologies nested within System…
Namespace Purpose Assembly
System Core classes, types mscorlib.dll
System.Collections Data structures mscorlib.dll
System.Data Database access System.Data.dll
System.Windows.Forms GUI System.Windows.Forms.dll
System.XML XML processing System.Xml.dll
http://ebooks.edhole.com
25Microsoft
Namespace != Assembly
• Orthogonal concepts:
– namespace for organization
– assembly for packaging
• One namespace could be spread across multiple assemblies
• One assembly may contain multiple namesspaces
– e.g. mscorlib.dll
http://ebooks.edhole.com
26Microsoft
Summary
• CTS is the common type system
– same type system for all languages
– types implemented by classes in FCL
– fundamental difference between value & reference types
• CLS is the common language specification
– types that are guaranteed to work across languages
• Try not to confuse namespaces with assemblies…
– namespaces help with organization
– assemblies denote implementation / packaging
http://ebooks.edhole.com
27Microsoft
References
• Books:
– I. Pohl, "C# by Dissection"
– S. Lippman, "C# Primer"
– J. Mayo, "C# Unleashed"
http://ebooks.edhole.com
Free Ebooks Download
Mba Ebooks
By Edhole
Mba ebooks
Free ebooks download
http://ebooks.edhole.co

More Related Content

More from Edhole.com

Chartered accountant in dwarka
Chartered accountant in dwarkaChartered accountant in dwarka
Chartered accountant in dwarkaEdhole.com
 
Ca firm in dwarka
Ca firm in dwarkaCa firm in dwarka
Ca firm in dwarkaEdhole.com
 
Website development company surat
Website development company suratWebsite development company surat
Website development company suratEdhole.com
 
Website designing company in surat
Website designing company in suratWebsite designing company in surat
Website designing company in suratEdhole.com
 
Website dsigning company in india
Website dsigning company in indiaWebsite dsigning company in india
Website dsigning company in indiaEdhole.com
 
Website designing company in delhi
Website designing company in delhiWebsite designing company in delhi
Website designing company in delhiEdhole.com
 
Chartered accountant in dwarka
Chartered accountant in dwarkaChartered accountant in dwarka
Chartered accountant in dwarkaEdhole.com
 
Ca firm in dwarka
Ca firm in dwarkaCa firm in dwarka
Ca firm in dwarkaEdhole.com
 
Website development company surat
Website development company suratWebsite development company surat
Website development company suratEdhole.com
 
Website designing company in surat
Website designing company in suratWebsite designing company in surat
Website designing company in suratEdhole.com
 
Website designing company in india
Website designing company in indiaWebsite designing company in india
Website designing company in indiaEdhole.com
 
Website designing company in delhi
Website designing company in delhiWebsite designing company in delhi
Website designing company in delhiEdhole.com
 
Website designing company in mumbai
Website designing company in mumbaiWebsite designing company in mumbai
Website designing company in mumbaiEdhole.com
 
Website development company surat
Website development company suratWebsite development company surat
Website development company suratEdhole.com
 
Website desinging company in surat
Website desinging company in suratWebsite desinging company in surat
Website desinging company in suratEdhole.com
 
Website designing company in india
Website designing company in indiaWebsite designing company in india
Website designing company in indiaEdhole.com
 
Website designing company in delhi
Website designing company in delhiWebsite designing company in delhi
Website designing company in delhiEdhole.com
 

More from Edhole.com (20)

Chartered accountant in dwarka
Chartered accountant in dwarkaChartered accountant in dwarka
Chartered accountant in dwarka
 
Ca in dwarka
Ca in dwarkaCa in dwarka
Ca in dwarka
 
Ca firm in dwarka
Ca firm in dwarkaCa firm in dwarka
Ca firm in dwarka
 
Website development company surat
Website development company suratWebsite development company surat
Website development company surat
 
Website designing company in surat
Website designing company in suratWebsite designing company in surat
Website designing company in surat
 
Website dsigning company in india
Website dsigning company in indiaWebsite dsigning company in india
Website dsigning company in india
 
Website designing company in delhi
Website designing company in delhiWebsite designing company in delhi
Website designing company in delhi
 
Ca in patna
Ca in patnaCa in patna
Ca in patna
 
Chartered accountant in dwarka
Chartered accountant in dwarkaChartered accountant in dwarka
Chartered accountant in dwarka
 
Ca firm in dwarka
Ca firm in dwarkaCa firm in dwarka
Ca firm in dwarka
 
Ca in dwarka
Ca in dwarkaCa in dwarka
Ca in dwarka
 
Website development company surat
Website development company suratWebsite development company surat
Website development company surat
 
Website designing company in surat
Website designing company in suratWebsite designing company in surat
Website designing company in surat
 
Website designing company in india
Website designing company in indiaWebsite designing company in india
Website designing company in india
 
Website designing company in delhi
Website designing company in delhiWebsite designing company in delhi
Website designing company in delhi
 
Website designing company in mumbai
Website designing company in mumbaiWebsite designing company in mumbai
Website designing company in mumbai
 
Website development company surat
Website development company suratWebsite development company surat
Website development company surat
 
Website desinging company in surat
Website desinging company in suratWebsite desinging company in surat
Website desinging company in surat
 
Website designing company in india
Website designing company in indiaWebsite designing company in india
Website designing company in india
 
Website designing company in delhi
Website designing company in delhiWebsite designing company in delhi
Website designing company in delhi
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

Free Ebooks Download ! Edhole

  • 1. Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download http://ebooks.edhole.co
  • 3. 3Microsoft Objectives “.NET is designed around the CTS, or Common Type System. The CTS is what allows assemblies, written in different languages, to work together. To ensure interoperability across languages, Microsoft has also defined the CLS, or Common Language Specification, a subset of the CTS that all languages support. Otherwise, the types in C# are what you would expect from a modern OOPL…” • The Common Type System • Value vs. reference types • Arrays • Namespaces http://ebooks.edhole.com
  • 4. 4Microsoft Part 1 • The Common Type System… http://ebooks.edhole.com
  • 5. 5Microsoft The Common Type System (CTS) • CTS is based on a hierarchy of classes defined in FCL – all types inherit from Object (all except interface types) String Array ValueType Exception Delegate Class1 Multicast Delegate Class2 Class3 Object Enum1 Structure1Enum Primitive types Boolean Byte Int16 Int32 Int64 Char Single Double Decimal DateTime System-defined types User-defined types Delegate1 TimeSpan Guid http://ebooks.edhole.com
  • 6. 6Microsoft The Common Language Specification (CLS) • Not all languages support all CTS types and features – C# supports unsigned integer types, VB.NET does not – C# is case sensitive, VB.NET is not – C# supports pointer types (in unsafe mode), VB.NET does not – C# supports operator overloading, VB.NET does not • CLS was drafted to promote language interoperability – vast majority of classes within FCL are CLS-compliant http://ebooks.edhole.com
  • 7. 7Microsoft Mapping C# to CTS • Language keywords map to common CTS classes: Keyword Description Special format for literals bool Boolean true false char 16 bit Unicode character 'A' 'x0041' 'u0041' sbyte 8 bit signed integer none byte 8 bit unsigned integer none short 16 bit signed integer none ushort 16 bit unsigned integer none int 32 bit signed integer none uint 32 bit unsigned integer U suffix long 64 bit signed integer L or l suffix ulong 64 bit unsigned integer U/u and L/l suffix float 32 bit floating point F or f suffix double 64 bit floating point no suffix decimal 128 bit high precision M or m suffix string character sequence "hello", @"C:dirfile.txt" http://ebooks.edhole.com
  • 8. 8Microsoft Example • An example of using types in C# – declare before you use (compiler enforced) – initialize before you use (compiler enforced) public class App { public static void Main() { int width, height; width = 2; height = 4; int area = width * height; int x; int y = x * 2; ... } } declarations decl + initializer error, x not set http://ebooks.edhole.com
  • 9. 9Microsoft Type conversion • Some automatic type conversions available – from smaller to larger types • Otherwise you need a cast or an explicit conversion… – typecast syntax is type name inside parentheses – conversion based on System.Convert class int i = 5; double d = 3.2; string s = "496"; d = i; i = (int) d; i = System.Convert.ToInt32(s); implicit conversion typecast required conversion required http://ebooks.edhole.com
  • 10. 10Microsoft Part 2 • Value vs. reference types… http://ebooks.edhole.com
  • 11. 11Microsoft Value vs. reference types • C# separates data types into two categories • Value types: – variable represents a value ("bits") • Reference types: – variable represents a reference to a heap-based object – actual data resides in the object int i; i = 10; 10 string s; s = "calico"; "calico" http://ebooks.edhole.com
  • 12. 12Microsoft How do you know which types are which? • Memorization! • Though it's pretty obvious based on past experience – primitive types like bool, int and double are values – remainder are reference types int i; string s; Customer c1, c2; i = 23; s = "a message"; c1 = null; c2 = new Customer(…); http://ebooks.edhole.com
  • 13. 13Microsoft Boxing and Unboxing • When necessary, C# will auto-convert value <==> object – value ==> object is called "boxing" – object ==> value is called "unboxing" int i, j; object obj; string s; i = 32; obj = i; // boxed copy! i = 19; j = (int) obj; // unboxed! s = j.ToString(); // boxed! s = 99.ToString(); // boxed! http://ebooks.edhole.com
  • 14. 14Microsoft User-defined reference types • Classes! – for example, Customer class we worked with earlier… public class Customer { public string Name; // fields public int ID; public Customer(string name, int id) // constructor { this.Name = name; this.ID = id; } public override string ToString() // method { return "Customer: " + this.Name; } } http://ebooks.edhole.com
  • 15. 15Microsoft Working with reference types… • Creating, assigning, and comparing: Customer c1, c2, c3; string s1, s2; c1 = new Customer("joe hummel", 36259); c2 = new Customer("marybeth lore", 55298); c3 = null; // c3 references no object c3 = c1; // c3 now references same obj as c1 if (c1 == null) ... // do I ref an object? if (c1 == c2) ... // compares references if (c1.Equals(c2)) ... // compares objects if (s1 == s2) ... // exception: == overloaded to // compare string data http://ebooks.edhole.com
  • 16. 16Microsoft Defining equality • Classes should override Equals public class Customer { . . . public override bool Equals(object obj) { Customer other; if ((obj == null) || (!(obj is Customer))) return false; // definitely not equal other = (Customer) obj; // typecast to access return this.ID == other.ID; // equal if same id... } http://ebooks.edhole.com
  • 17. 17Microsoft GetHashCode • If you override Equals, must also override GetHashCode: public class Customer { . . . public override int GetHashCode() { return this.id.GetHashCode(); } http://ebooks.edhole.com
  • 19. 19Microsoft Arrays • Arrays are reference types – based on Array class in FCL – must be created using new – 0-based indexing – assigned default values (0 for numeric, null for references, etc.) int[] a; a = new int[5]; a[0] = 17; a[1] = 32; int x = a[0] + a[1] + a[4]; int l = a.Length; element access create number of elements http://ebooks.edhole.com
  • 20. 20Microsoft Multi-dimensional arrays • C# supports arrays as a single object OR array of arrays – latter allows you to implement jagged arrays Customer[,] twoD; int[][] jagged2D; // 2D array as single object twoD = new Customer[10, 100]; twoD[0, 0] = new Customer(…); twoD[9, 99] = new Customer(…); // 2D array as array of arrays jagged2D = new int[10][]; jagged2D[0] = new int[10]; jagged2D[1] = new int[20]; jagged2D[9] = new int[100]; jagged2D[0][0] = 1; jagged2D[9][99] = 100; http://ebooks.edhole.com
  • 22. 22Microsoft Namespaces • Namespaces are a means for organizing types – a namespace N is a set of names scoped by N – namespaces are often nested namespace Workshop { public class Customer { . . . } public class Product { . . . } }//namespace Workshop.Customer http://ebooks.edhole.com
  • 23. 23Microsoft Example • Framework Class Library (FCL) contains 1000's of classes – how to organize? – how to avoid name collisions? • with FCL • within FCL http://ebooks.edhole.com
  • 24. 24Microsoft FCL namespaces • FCL's outermost namespace is "System" • FCL technologies nested within System… Namespace Purpose Assembly System Core classes, types mscorlib.dll System.Collections Data structures mscorlib.dll System.Data Database access System.Data.dll System.Windows.Forms GUI System.Windows.Forms.dll System.XML XML processing System.Xml.dll http://ebooks.edhole.com
  • 25. 25Microsoft Namespace != Assembly • Orthogonal concepts: – namespace for organization – assembly for packaging • One namespace could be spread across multiple assemblies • One assembly may contain multiple namesspaces – e.g. mscorlib.dll http://ebooks.edhole.com
  • 26. 26Microsoft Summary • CTS is the common type system – same type system for all languages – types implemented by classes in FCL – fundamental difference between value & reference types • CLS is the common language specification – types that are guaranteed to work across languages • Try not to confuse namespaces with assemblies… – namespaces help with organization – assemblies denote implementation / packaging http://ebooks.edhole.com
  • 27. 27Microsoft References • Books: – I. Pohl, "C# by Dissection" – S. Lippman, "C# Primer" – J. Mayo, "C# Unleashed" http://ebooks.edhole.com
  • 28. Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download http://ebooks.edhole.co