[RESOLVED] Questions about casting object types...
Hi there, ladies and gents.
I am still in the full throws of learning C# and OOP; however, I'm not actively using it on a day-to-day basis, so my knowledge is based on books rather than real-world programming.
I have a concept I feel I understand (I hope) - casting - but I wanted to reaffirm my understanding with you experienced C#/OOP programmers to make sure I am on the right track. I'd be very grateful if I could get your input.
Please forgive my explanation; I hope it is easy to understand.
Question 1:
Consider the following statement:
Code:
Object Obj = new Employee();
As I understand it, the above statement declares a reference variable of type Object, which is assigned a memory address pointing to an Employee object created by the new keyword. Am I correct in thinking that the the Obj reference variable cannot see the implementation details of the Employee object, only those defined by Object?
Questions 2:
Consider the following statement:
Code:
Employee emp = Obj;
Am I correct in thinking that the above statement would cause an invalid casting exception error given this attempted implicit cast?
And to rectify this, I'd need to perform an explicit cast?
Code:
Employee emp = (Employee)Obj;
Does the above cast cause both reference variables emp and Obj to point to the same object? Would their 'view' of the object differ based on their declared types?
Given the explicit cast, can emp now see the full interface details defined in the Employee class?
Many thanks in advance for your help, :thumb:
Re: Questions about casting object types...
Question 1:
Am I correct in thinking that the the Obj reference variable cannot see the implementation details of the Employee object, only those defined by Object?
Yes you are are correct. Obj reference cannot see the implementation details of the Employee object unless you typecast it to Employee.
For example you Employee class is
Code:
class Employee
{
public string EmpName
{
return "MyName";
}
}
Then , take a look here
Code:
Object Obj = new Employee();
print(Obj.EmpName); // Compiler Error here because obj cannot see EmpName yet
print(((Employee)Obj).EmpName); // Works fine
Re: Questions about casting object types...
Questions 2:
Consider the following statement:
Code:
Employee emp = Obj;
Am I correct in thinking that the above statement would cause an invalid casting exception error given this attempted implicit cast?
Definitely, the compiler will give Invalid Cast error. You will have to explicitly type cast it.
Re: Questions about casting object types...
Code:
Employee emp = (Employee)Obj;
-- Edit --
I meant to quote the following:
object o = new Employee ();
Employee e = o;
-- End edit --
An exception won't be thrown, it'll be a compile time error. However, if you wrote:
Code:
int a = 5;
object o = a;
Employee employee = (Employee) o;
That would compile, but you'd get a runtime error (invalid cast exception).
Re: Questions about casting object types...
Quote:
Originally Posted by
Mutant_Fruit
Code:
Employee emp = (Employee)Obj;
An exception won't be thrown, it'll be a compile time error.
However, if you wrote:
Code:
int a = 5;
object o = a;
Employee employee = (Employee) o;
That would compile, but you'd get a runtime error (invalid cast exception).
I thought this explicit cast
Code:
Employee emp = (Employee)Obj;
would work given the Obj reference variable points to an Employee object?
Re: Questions about casting object types...
Quote:
Originally Posted by
nabeelisnabeel
Question 1:
Am I correct in thinking that the the Obj reference variable cannot see the implementation details of the Employee object, only those defined by Object?
Yes you are are correct. Obj reference cannot see the implementation details of the Employee object unless you typecast it to Employee.
For example you Employee class is
Code:
class Employee
{
public string EmpName
{
return "MyName";
}
}
Then , take a look here
Code:
Object Obj = new Employee();
print(Obj.EmpName); // Compiler Error here because obj cannot see EmpName yet
print(((Employee)Obj).EmpName); // Works fine
Thanks for the response! ;)
Re: Questions about casting object types...
Quote:
Originally Posted by
Mutant_Fruit
Code:
Employee emp = (Employee)Obj;
An exception won't be thrown, it'll be a compile time error. However, if you wrote:
Code:
int a = 5;
object o = a;
Employee employee = (Employee) o;
That would compile, but you'd get a runtime error (invalid cast exception).
Thanks for the response! ;)
Re: Questions about casting object types...
Whoops, i quoted the wrong thing. I meant to say:
Code:
object o = new Employee ();
Employee e = o;
That's a compile time error as object is not a subclass of Employee.
Re: Questions about casting object types...
Quote:
Originally Posted by
Mutant_Fruit
Whoops, i quoted the wrong thing. I meant to say:
Code:
object o = new Employee ();
Employee e = o;
That's a compile time error as object is not a subclass of Employee.
So this is a valid statement and won't cause an exception at runtime?
Code:
Employee emp = (Employee)Obj;
Thanks again for your help.
Re: Questions about casting object types...
Well, it may cause an exception at runtime of 'Obj' is not an Employee object under the hood. You would be better served using a safe cast on reference types:
Code:
object o = new Employee();
// some time later
Employee e = o as Employee;
if ( e != null )
{
// the cast succeeded
}
else
{
// the cast failed, 'o' is not of the type 'Employee'
}
Re: Questions about casting object types...
Quote:
Originally Posted by
BigEd781
Well, it may cause an exception at runtime of 'Obj' is not an Employee object under the hood. You would be better served using a safe cast on reference types:
Code:
object o = new Employee();
// some time later
Employee e = o as Employee;
if ( e != null )
{
// the cast succeeded
}
else
{
// the cast failed, 'o' is not of the type 'Employee'
}
Understood. Thanks. ;)