Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Aug 11, 2008 10:00 PM by SGWellens
Member
32 Points
207 Posts
Aug 11, 2008 06:04 PM|LINK
Hi
Is this a way of boxing? Can you please explain what it's is please? This code is inside a method which contains parameters.
string n1 = ((ClassName)MethodParam).ClassMember;
Thanks,
E
All-Star
126029 Points
10309 Posts
Moderator
Aug 11, 2008 10:00 PM|LINK
Ennair Is this a way of boxing?
No.
MethodParam is a base class type. However, what is being passed is a derived type.
To get to the derived type member, first MethodParam is cast to type ClassName, then ClassName's properties are available.
The parenthesis are needed because the dot operator has higher precedence than the cast operator.
Example: Say there is a base class called Animal and a class called Dog that derives from it. Not all Animals have Collars but Dogs do...so:
String Collar = ((Dog)Animal).Collar;
Ennair
Member
32 Points
207 Posts
C# Syntax
Aug 11, 2008 06:04 PM|LINK
Hi
Is this a way of boxing? Can you please explain what it's is please? This code is inside a method which contains parameters.
string n1 = ((ClassName)MethodParam).ClassMember;
Thanks,
E
SGWellens
All-Star
126029 Points
10309 Posts
Moderator
Re: C# Syntax
Aug 11, 2008 10:00 PM|LINK
No.
MethodParam is a base class type. However, what is being passed is a derived type.
To get to the derived type member, first MethodParam is cast to type ClassName, then ClassName's properties are available.
The parenthesis are needed because the dot operator has higher precedence than the cast operator.
Example: Say there is a base class called Animal and a class called Dog that derives from it. Not all Animals have Collars but Dogs do...so:
String Collar = ((Dog)Animal).Collar;
My blog