Object Typecasting Data Structure - StudySection

About This Presentation
Title:

Object Typecasting Data Structure - StudySection

Description:

Object Typecasting is a process of converting one data structure into another. The type system is categorized into 2 types, primitives and references. –

Number of Views:8
Slides: 9
Provided by: studysection

less

Transcript and Presenter's Notes

Title: Object Typecasting Data Structure - StudySection


1
Object Typecasting
  • WWW.STUDYSECTION.COM

2
Introduction
  • Object Typecasting is a process of converting one
    data structure into another. The type system is
    categorized into 2 types, primitives and
    references.
  • Primitive types are also known as value types,
    which means a variable contains the value itself.
    Most simply type data structure would look like
    this.

3
  • In this figure, we can see that primitive data
    types are stored inside stack memory and the
    variables point to a memory location. The
    variable value is placed next to the variable
    label in memory.
  • On the other hand, reference types hold reference
    or address of the object data, however, that
    address binding with the label of variable is
    inside the stack memory, opposite to this
    variable data is stored inside Heap memory.

4
Type Casting Rules
Compile-time checking 1
  • Type of D (refers to the General typecasting
    equation) and C must have some relation in
    between them. Either child to parent or parent to
    child or must be of the same type, otherwise, we
    will get a compile-time error.
  • Case 1.
  • Object o "Hello World"
  • StringBuilder sb (StringBuilder)o
  • This code will compile successfully. The object
    is the topmost class or parent of all types. A
    string of data can easily hold an Object type.
  • After that, object and StringBuilder have a
    parent/child relationship, however, to convert
    from parent to child type, we need to explicitly
    mention the typecasting.
  • Case 2.
  • string s "Hello World"
  • StringBuilder sb (StringBuilder)s
  • This code will raise a compile-time error in the
    second line, where we are explicitly converting a
    string to the StringBuilder type. There is no
    relationship between string and StringBuilder
    type.
  • Error description
  • Compile Time Error Cannot convert type string
    to System.Text.StringBuilder

5
Compile-time checking 2
  • C must be either the same or derived/child type
    of A, otherwise we will get a compile-time
    error.
  • Case 1.
  • Object o "Hello World"
  • StringBuilder sb (StringBuilder)o
  • This code will compile successfully.
  • Case 2.
  • Object o "Hello World"
  • StringBuilder sb (string)o
  • The string is not a derived class of
    StringBuilder and not the same at all, so this
    code fails to follow rule Compile-time checking
    2. Consequently, it will raise a compile-time
    error.
  • Error description
  • Compile Time Error
  • Cannot implicitly convert type string to
    System.Text.StringBuilder

6
Runtime checking
  • Type of D must be either the same or derived
    type of C, otherwise, we will get a runtime
    exception,
  • Case 1.
  • Object o "Hello World"
  • StringBuilder sb (StringBuilder)o
  • This code will compile with no errors. However,
    when we try to run it, it will throw a runtime
    exception.
  • At runtime o will be of string type, which
    means, when the runtime environment will execute
    a second statement, it has to convert a string
    type variable to a StringBuilder type and we have
    already seen that there is no any relationship
    between them.
  • Error Description
  • Runtime Exception System.InvalidCastException
  • Unable to cast object of type System.String to
    type System.Text.StringBuilder
  • Case 2.
  • What do you think, will the following code
    snippet compile and run without any issue.
  • Object o "Hello World"
  • Object o1 (string)o
  • At runtime, o will be of type string the same as
    string, resulting in a successful type
    conversion and the program runs smoothly.

7
Online Testing Certification Exam StudySection
offers Testing Online Certifications at
Foundation Advanced Expert levels apart from
many other free online certification exams.
8
About StudySection
Welcome to StudySection - the most loved online
platform for eCertification in several subjects
including but not limited to Software
Development, Quality Assurance, Business
Administration, Project Management, English,
Aptitude and more. From more than 70 countries
students are StudySection Certified. If you are
not yet StudySection certified it's not late. You
can start right now. Being StudySection
Certified helps you take your education level few
notches up and have an edge over other candidates
when you need it the most. Globally, our students
are employed in different organizations and are
utilizing the benefit of being certified with us.
Write a Comment
User Comments (0)
About PowerShow.com