SlideShare a Scribd company logo
1 of 33
Using Reference-Type
Variables
Overview
 Using Reference-Type Variables
 Using Common Reference Types
 The Object Hierarchy
 Namespaces in the .NET Framework
 Data Conversions
Using Reference-Type Variables
 Comparing Value Types to Reference Types
 Declaring and Releasing Reference Variables
 Invalid References
 Comparing Values and Comparing References
 Multiple References to the Same Object
 Using References as Method Parameters
Comparing Value Types to Reference Types
 Value types
 The variable
contains the value
directly
 Examples:
char, int
42
int mol;
mol = 42;
•
string mol;
mol = "Hello";
Hello
 Reference types
 The variable contains a
reference to the data
 Data is stored in a
separate memory area
Declaring and Releasing Reference Variables
 Declaring reference variables
coordinate c1;
c1 = new coordinate();
c1.x = 6.12;
c1.y = 4.2;
• 6.12 4.2
c1 = null;
• 6.12 4.2
 Releasing reference variables
Invalid References
 If you have invalid references
 You cannot access members or variables
 Invalid references at compile time
 Compiler detects use of uninitialized references
 Invalid references at run time
 System will generate an exception error
Comparing Values and
Comparing References
 Comparing value types
 == and != compare values
 Comparing reference types
 == and != compare the references, not the values
• 1.0 2.0
• 1.0 2.0
Different
Multiple References to the Same
Object
 Two references can refer to the same object
 Two ways to access the same object for read/write
coordinate c1= new coordinate( );
coordinate c2;
c1.x = 2.3; c1.y = 7.6;
c2 = c1;
Console.WriteLine(c1.x + " , " + c1.y);
Console.WriteLine(c2.x + " , " + c2.y);
•
2.3 7.6
•
c1
c2
Using References as Method
Parameters
 References can be used as parameters
 When passed by value, data being referenced may be
changed
static void PassCoordinateByValue(coordinate c)
{
c.x++; c.y++;
}
loc.x = 2; loc.y = 3;
PassCoordinateByValue(loc);
Console.WriteLine(loc.x + " , " + loc.y);
2 3 3 4
•
•
Using Common Reference Types
 Exception Class
 String Class
 Common String Methods, Operators, and Properties
 String Comparisons
 String Comparison Operators
Exception Class
 Exception is a class
 Exception objects are used to raise exceptions
 Create an Exception object by using new
 Throw the object by using throw
 Exception types are subclasses of Exception
String Class
 Multiple character Unicode data
 Shorthand for System.String
 Immutable
string s = "Hello";
s[0] = 'c'; // Compile-time error
Common String Methods,
Operators, and Properties
 Brackets
 Insert method
 Length property
 Copy method
 Concat method
 Trim method
 ToUpper and ToLower methods
String Comparisons
 Equals method
 Value comparison
 Compare method
 More comparisons
 Case-insensitive option
 Dictionary ordering
 Locale-specific compare options
String Comparison Operators
 The == and != operators are overloaded for strings
 They are equivalent to String.Equals and !String.Equals
string a = "Test";
string b = "Test";
if (a == b) ... // Returns true
The Object Hierarchy
 The object Type
 Common Methods
 Reflection
The object Type
 Synonym for System.Object
 Base class for all classes
Exception
SystemException
MyClass
Object
String
Common Methods
 Common methods for all reference types
 ToString method
 Equals method
 GetType method
 Finalize method
 GetHashCode method
Reflection
 You can query the type of an object
 System.Reflection namespace
 The typeof operator returns a type object
 Compile-time classes only
 GetType method in System.Object
 Run-time class information
 Namespaces in the .NET
Framework
 System.IO Namespace
 System.Xml Namespace
 System.Data Namespace
 Other Useful Namespaces
System.IO Namespace
 Access to file system input/output
 File, Directory
 StreamReader, StreamWriter
 FileStream
 BinaryReader, BinaryWriter
System.Xml Namespace
 XML support
 Various XML-related standards
System.Data Namespace
 System.Data.SqlClient
 SQL Server .NET Data Provider
 System.Data
 Consists mostly of the classes that constitute the ADO.NET
architecture
Other Useful Namespaces
 System namespace
 System.Net namespace
 System.Net.Sockets namespace
 System.Windows.Forms namespace
Data Conversions
 Converting Value Types
 Parent/Child Conversions
 The is Operator
 The as Operator
 Conversions and the object Type
 Conversions and Interfaces
 Boxing and Unboxing
Converting Value Types
 Implicit conversions
 Explicit conversions
 Cast operator
 Exceptions
 System.Convert class
 Handles the conversions internally
Parent/Child Conversions
 Conversion to parent class reference
 Implicit or explicit
 Always succeeds
 Can always assign to object
 Conversion to child class reference
 Explicit casting required
 Will check that the reference is of the correct type
 Will raise InvalidCastException if not
The is Operator
 Returns true if a conversion can be made
Bird b;
if (a is Bird)
b = (Bird) a; // Safe
else
Console.WriteLine("Not a Bird");
The as Operator
 Converts between reference types, like cast
 On error
 Returns null
 Does not raise an exception
Bird b = a as Bird; // Convert
if (b == null)
Console.WriteLine("Not a bird");
Conversions and the object Type
 The object type is the base for all classes
 Any reference can be assigned to object
 Any object variable can be assigned to any reference
 With appropriate type conversion and checks
 The object type and is operator
object ox;
ox = a;
ox = (object) a;
ox = a as object;
b = (Bird) ox;
b = ox as Bird;
Conversion and Interfaces
 An interface can only be used to access its own
members
 Other methods and variables of the class are not
accessible through the interface
Boxing and Unboxing
 Unified type system
 Boxing
 Unboxing
 Calling object methods on value types
int p = 123;
object box;
box = p;
• 123
123 p = (int)box;
Review
 Using Reference-Type Variables
 Using Common Reference Types
 The Object Hierarchy
 Namespaces in the .NET Framework
 Data Conversions

More Related Content

What's hot (19)

PHP | ZENUS INFOTECH INDIA PVT. LTD.
PHP | ZENUS INFOTECH INDIA PVT. LTD. PHP | ZENUS INFOTECH INDIA PVT. LTD.
PHP | ZENUS INFOTECH INDIA PVT. LTD.
 
Java Tutorial Lab 5
Java Tutorial Lab 5Java Tutorial Lab 5
Java Tutorial Lab 5
 
Of Lambdas and LINQ
Of Lambdas and LINQOf Lambdas and LINQ
Of Lambdas and LINQ
 
From OOP to FP : the validation case
From OOP to FP : the validation caseFrom OOP to FP : the validation case
From OOP to FP : the validation case
 
Code quality
Code qualityCode quality
Code quality
 
Resharper - Next Steps
Resharper - Next StepsResharper - Next Steps
Resharper - Next Steps
 
Java 8
Java 8Java 8
Java 8
 
Introduction to JSX
Introduction to JSXIntroduction to JSX
Introduction to JSX
 
IOS Swift language 2nd tutorial
IOS Swift language 2nd tutorialIOS Swift language 2nd tutorial
IOS Swift language 2nd tutorial
 
C#/.NET Little Pitfalls
C#/.NET Little PitfallsC#/.NET Little Pitfalls
C#/.NET Little Pitfalls
 
Java annotations
Java annotationsJava annotations
Java annotations
 
#_ varible function
#_ varible function #_ varible function
#_ varible function
 
Variable
VariableVariable
Variable
 
The Many Faces of Swift Functions
The Many Faces of Swift FunctionsThe Many Faces of Swift Functions
The Many Faces of Swift Functions
 
JetBrains ReSharper
JetBrains ReSharperJetBrains ReSharper
JetBrains ReSharper
 
DIG1108C Lesson 5 Fall 2014
DIG1108C Lesson 5 Fall 2014DIG1108C Lesson 5 Fall 2014
DIG1108C Lesson 5 Fall 2014
 
L3 operators
L3 operatorsL3 operators
L3 operators
 
JetBrains ReSharper
JetBrains ReSharperJetBrains ReSharper
JetBrains ReSharper
 
Lambdas
LambdasLambdas
Lambdas
 

Similar to Module 9 : using reference type variables

Similar to Module 9 : using reference type variables (20)

CIS160 final review
CIS160 final reviewCIS160 final review
CIS160 final review
 
C# language basics (Visual Studio)
C# language basics (Visual Studio) C# language basics (Visual Studio)
C# language basics (Visual Studio)
 
C# language basics (Visual studio)
C# language basics (Visual studio)C# language basics (Visual studio)
C# language basics (Visual studio)
 
Basic of java 2
Basic of java  2Basic of java  2
Basic of java 2
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
 
Java Basics
Java BasicsJava Basics
Java Basics
 
C0 review core java1
C0 review core java1C0 review core java1
C0 review core java1
 
Introduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh SinghIntroduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh Singh
 
Chapter 2 c#
Chapter 2 c#Chapter 2 c#
Chapter 2 c#
 
Intro To Scala
Intro To ScalaIntro To Scala
Intro To Scala
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variables
 
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITKAdvanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
 
Java Basics
Java BasicsJava Basics
Java Basics
 
C# chap 4
C# chap 4C# chap 4
C# chap 4
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
 
Ap Power Point Chpt5
Ap Power Point Chpt5Ap Power Point Chpt5
Ap Power Point Chpt5
 
Cis266 final review
Cis266 final reviewCis266 final review
Cis266 final review
 
Synapseindia strcture of dotnet development part 2
Synapseindia strcture of dotnet development part 2Synapseindia strcture of dotnet development part 2
Synapseindia strcture of dotnet development part 2
 

More from Prem Kumar Badri

Module 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopeModule 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopePrem Kumar Badri
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and eventsPrem Kumar Badri
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objectsPrem Kumar Badri
 
Module 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsModule 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsPrem Kumar Badri
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingPrem Kumar Badri
 
Module 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsModule 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsPrem Kumar Badri
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parametersPrem Kumar Badri
 
Module 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformModule 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformPrem Kumar Badri
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collectionPrem Kumar Badri
 
C# Filtering in generic collections
C# Filtering in generic collectionsC# Filtering in generic collections
C# Filtering in generic collectionsPrem Kumar Badri
 

More from Prem Kumar Badri (20)

Module 15 attributes
Module 15 attributesModule 15 attributes
Module 15 attributes
 
Module 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopeModule 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scope
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
 
Module 11 : Inheritance
Module 11 : InheritanceModule 11 : Inheritance
Module 11 : Inheritance
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objects
 
Module 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsModule 8 : Implementing collections and generics
Module 8 : Implementing collections and generics
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented Programming
 
Module 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsModule 5 : Statements & Exceptions
Module 5 : Statements & Exceptions
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parameters
 
Module 2: Overview of c#
Module 2:  Overview of c#Module 2:  Overview of c#
Module 2: Overview of c#
 
Module 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformModule 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET Platform
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collection
 
C# Multi threading
C# Multi threadingC# Multi threading
C# Multi threading
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
C# Generic collections
C# Generic collectionsC# Generic collections
C# Generic collections
 
C# Global Assembly Cache
C# Global Assembly CacheC# Global Assembly Cache
C# Global Assembly Cache
 
C# Filtering in generic collections
C# Filtering in generic collectionsC# Filtering in generic collections
C# Filtering in generic collections
 
C# File IO Operations
C# File IO OperationsC# File IO Operations
C# File IO Operations
 

Recently uploaded

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Recently uploaded (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

Module 9 : using reference type variables

  • 2. Overview  Using Reference-Type Variables  Using Common Reference Types  The Object Hierarchy  Namespaces in the .NET Framework  Data Conversions
  • 3. Using Reference-Type Variables  Comparing Value Types to Reference Types  Declaring and Releasing Reference Variables  Invalid References  Comparing Values and Comparing References  Multiple References to the Same Object  Using References as Method Parameters
  • 4. Comparing Value Types to Reference Types  Value types  The variable contains the value directly  Examples: char, int 42 int mol; mol = 42; • string mol; mol = "Hello"; Hello  Reference types  The variable contains a reference to the data  Data is stored in a separate memory area
  • 5. Declaring and Releasing Reference Variables  Declaring reference variables coordinate c1; c1 = new coordinate(); c1.x = 6.12; c1.y = 4.2; • 6.12 4.2 c1 = null; • 6.12 4.2  Releasing reference variables
  • 6. Invalid References  If you have invalid references  You cannot access members or variables  Invalid references at compile time  Compiler detects use of uninitialized references  Invalid references at run time  System will generate an exception error
  • 7. Comparing Values and Comparing References  Comparing value types  == and != compare values  Comparing reference types  == and != compare the references, not the values • 1.0 2.0 • 1.0 2.0 Different
  • 8. Multiple References to the Same Object  Two references can refer to the same object  Two ways to access the same object for read/write coordinate c1= new coordinate( ); coordinate c2; c1.x = 2.3; c1.y = 7.6; c2 = c1; Console.WriteLine(c1.x + " , " + c1.y); Console.WriteLine(c2.x + " , " + c2.y); • 2.3 7.6 • c1 c2
  • 9. Using References as Method Parameters  References can be used as parameters  When passed by value, data being referenced may be changed static void PassCoordinateByValue(coordinate c) { c.x++; c.y++; } loc.x = 2; loc.y = 3; PassCoordinateByValue(loc); Console.WriteLine(loc.x + " , " + loc.y); 2 3 3 4 • •
  • 10. Using Common Reference Types  Exception Class  String Class  Common String Methods, Operators, and Properties  String Comparisons  String Comparison Operators
  • 11. Exception Class  Exception is a class  Exception objects are used to raise exceptions  Create an Exception object by using new  Throw the object by using throw  Exception types are subclasses of Exception
  • 12. String Class  Multiple character Unicode data  Shorthand for System.String  Immutable string s = "Hello"; s[0] = 'c'; // Compile-time error
  • 13. Common String Methods, Operators, and Properties  Brackets  Insert method  Length property  Copy method  Concat method  Trim method  ToUpper and ToLower methods
  • 14. String Comparisons  Equals method  Value comparison  Compare method  More comparisons  Case-insensitive option  Dictionary ordering  Locale-specific compare options
  • 15. String Comparison Operators  The == and != operators are overloaded for strings  They are equivalent to String.Equals and !String.Equals string a = "Test"; string b = "Test"; if (a == b) ... // Returns true
  • 16. The Object Hierarchy  The object Type  Common Methods  Reflection
  • 17. The object Type  Synonym for System.Object  Base class for all classes Exception SystemException MyClass Object String
  • 18. Common Methods  Common methods for all reference types  ToString method  Equals method  GetType method  Finalize method  GetHashCode method
  • 19. Reflection  You can query the type of an object  System.Reflection namespace  The typeof operator returns a type object  Compile-time classes only  GetType method in System.Object  Run-time class information
  • 20.  Namespaces in the .NET Framework  System.IO Namespace  System.Xml Namespace  System.Data Namespace  Other Useful Namespaces
  • 21. System.IO Namespace  Access to file system input/output  File, Directory  StreamReader, StreamWriter  FileStream  BinaryReader, BinaryWriter
  • 22. System.Xml Namespace  XML support  Various XML-related standards
  • 23. System.Data Namespace  System.Data.SqlClient  SQL Server .NET Data Provider  System.Data  Consists mostly of the classes that constitute the ADO.NET architecture
  • 24. Other Useful Namespaces  System namespace  System.Net namespace  System.Net.Sockets namespace  System.Windows.Forms namespace
  • 25. Data Conversions  Converting Value Types  Parent/Child Conversions  The is Operator  The as Operator  Conversions and the object Type  Conversions and Interfaces  Boxing and Unboxing
  • 26. Converting Value Types  Implicit conversions  Explicit conversions  Cast operator  Exceptions  System.Convert class  Handles the conversions internally
  • 27. Parent/Child Conversions  Conversion to parent class reference  Implicit or explicit  Always succeeds  Can always assign to object  Conversion to child class reference  Explicit casting required  Will check that the reference is of the correct type  Will raise InvalidCastException if not
  • 28. The is Operator  Returns true if a conversion can be made Bird b; if (a is Bird) b = (Bird) a; // Safe else Console.WriteLine("Not a Bird");
  • 29. The as Operator  Converts between reference types, like cast  On error  Returns null  Does not raise an exception Bird b = a as Bird; // Convert if (b == null) Console.WriteLine("Not a bird");
  • 30. Conversions and the object Type  The object type is the base for all classes  Any reference can be assigned to object  Any object variable can be assigned to any reference  With appropriate type conversion and checks  The object type and is operator object ox; ox = a; ox = (object) a; ox = a as object; b = (Bird) ox; b = ox as Bird;
  • 31. Conversion and Interfaces  An interface can only be used to access its own members  Other methods and variables of the class are not accessible through the interface
  • 32. Boxing and Unboxing  Unified type system  Boxing  Unboxing  Calling object methods on value types int p = 123; object box; box = p; • 123 123 p = (int)box;
  • 33. Review  Using Reference-Type Variables  Using Common Reference Types  The Object Hierarchy  Namespaces in the .NET Framework  Data Conversions