Click here to Skip to main content
15,889,877 members
Home / Discussions / C#
   

C#

 
Questionhow to build my own Outlook? Pin
Jassim Rahma29-Aug-08 5:22
Jassim Rahma29-Aug-08 5:22 
AnswerRe: how to build my own Outlook? Pin
Kevin Marois29-Aug-08 8:02
professionalKevin Marois29-Aug-08 8:02 
AnswerRe: how to build my own Outlook? Pin
Pete O'Hanlon29-Aug-08 8:40
mvePete O'Hanlon29-Aug-08 8:40 
GeneralRe: how to build my own Outlook? Pin
Jassim Rahma29-Aug-08 10:22
Jassim Rahma29-Aug-08 10:22 
GeneralRe: how to build my own Outlook? Pin
Wendelius29-Aug-08 10:37
mentorWendelius29-Aug-08 10:37 
GeneralRe: how to build my own Outlook? Pin
Pete O'Hanlon30-Aug-08 11:14
mvePete O'Hanlon30-Aug-08 11:14 
QuestionInheritance and type casting Pin
Dewald29-Aug-08 4:14
Dewald29-Aug-08 4:14 
AnswerRe: Inheritance and type casting Pin
Scott Dorman29-Aug-08 4:48
professionalScott Dorman29-Aug-08 4:48 
Dewald wrote:
Given ClassB cb = new ClassB();
Is there a difference between:
ClassA ca = cb as ClassA;
and
ClassA ca = (ClassA)cb;


The difference here is that the first statement (ClassA ca = cb as ClassA;) will perform a cast using the <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.isinst.aspx">isinst</a>[<a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.isinst.aspx" target="_blank" title="New Window">^</a>]IL instruction. What this does for you is that it performs a "safe" cast in that if cb implements ClassA or is a derived class of ClassA it is cast to that type; otherwise it will return null. Also, if cb were null already, then a null is also returned.

The ClassA ca = (ClassA)cb; statement performs a cast using the <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.castclass.aspx">castclass</a>[<a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.castclass.aspx" target="_blank" title="New Window">^</a>] IL instruction. If cb implements ClassA or is a derived class of ClassA it is cast to that type; otherwise it will return an InvalidCastException. Also, if cb were null already, then a new object is returned set to a null reference is also returned.

Dewald wrote:
Is there a way to typecast the other way round? In other words, I have an instance of ClassA and I want to use it and view it as if it is of ClassB.


Given your examples I don't believe this is possible. If ClassA were to derive from ClassB, you could "downcast" to your base class or "upcast" to a more derived class, but those will only work in certain circumstances and you may not always get the expected results. It sounds like what you are looking for is the concept of covariance and contravariance[^], which currently do not exist in C# in a form that would allow you to do this.

Dewald wrote:
What is the difference between "type casting" and "boxing"? The latter is a term that's new to me and I get the impression that it might be different terms for the same concept? Am I wrong?


Boxing is Microsoft's term for encapsulating a value type inside a reference type (usually System.Object). Boxing an instance of a value type is performed in C# by typecasting it to a reference type. An instance of the reference type is created (on the stack), containing a copy of the original value. That implies a stack allocation, plus the garbage collector needs to keep track of the new reference type. Unboxing a value is performed by typecasting the value back to the original type, thereby retrieving the actual value from within the reference.

Scott Dorman
Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA

[Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

AnswerRe: Inheritance and type casting Pin
Abhishek Sur29-Aug-08 4:50
professionalAbhishek Sur29-Aug-08 4:50 
QuestionAttempted to read or write protected memory. This is often an indication that other memory is corrupt. Pin
Shpendh29-Aug-08 4:10
Shpendh29-Aug-08 4:10 
AnswerRe: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Pin
Pete O'Hanlon29-Aug-08 4:40
mvePete O'Hanlon29-Aug-08 4:40 
GeneralRe: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Pin
Shpendh29-Aug-08 5:08
Shpendh29-Aug-08 5:08 
GeneralRe: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Pin
lisan_al_ghaib29-Aug-08 5:59
lisan_al_ghaib29-Aug-08 5:59 
QuestionSearching for a windows form repeater-like method Pin
WhoisUrBuddy29-Aug-08 2:54
WhoisUrBuddy29-Aug-08 2:54 
AnswerRe: Searching for a windows form repeater-like method Pin
Abhishek Sur29-Aug-08 4:55
professionalAbhishek Sur29-Aug-08 4:55 
QuestionDropDownList Property Pin
dataminers29-Aug-08 1:49
dataminers29-Aug-08 1:49 
AnswerRe: DropDownList Property Pin
JoeSharp29-Aug-08 2:16
JoeSharp29-Aug-08 2:16 
QuestionHow to communicate with another application from my application? Pin
Tridip Bhattacharjee29-Aug-08 1:49
professionalTridip Bhattacharjee29-Aug-08 1:49 
AnswerRe: How to communicate with another application from my application? Pin
Abhijit Jana29-Aug-08 1:54
professionalAbhijit Jana29-Aug-08 1:54 
GeneralRe: How to communicate with another application from my application? Pin
Tridip Bhattacharjee31-Aug-08 19:54
professionalTridip Bhattacharjee31-Aug-08 19:54 
AnswerRe: How to communicate with another application from my application? Pin
Abhishek Sur29-Aug-08 4:59
professionalAbhishek Sur29-Aug-08 4:59 
QuestionScreen Capture of WebPages Pin
bhavin chheda29-Aug-08 1:34
bhavin chheda29-Aug-08 1:34 
QuestionDDE communication Pin
San29-Aug-08 1:18
San29-Aug-08 1:18 
AnswerRe: DDE communication Pin
Manas Bhardwaj29-Aug-08 1:26
professionalManas Bhardwaj29-Aug-08 1:26 
GeneralRe: DDE communication Pin
San29-Aug-08 2:04
San29-Aug-08 2:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.