Click here to Skip to main content
15,914,111 members
Home / Discussions / C#
   

C#

 
GeneralRe: Generic method: Cast an object to a return type T Pin
_groo_9-Apr-09 1:47
_groo_9-Apr-09 1:47 
QuestionAssignability of output parameters in C# Pin
dojohansen8-Apr-09 22:55
dojohansen8-Apr-09 22:55 
AnswerRe: Assignability of output parameters in C# Pin
Rob Philpott8-Apr-09 23:36
Rob Philpott8-Apr-09 23:36 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen9-Apr-09 0:21
dojohansen9-Apr-09 0:21 
AnswerRe: Assignability of output parameters in C# Pin
S. Senthil Kumar9-Apr-09 4:13
S. Senthil Kumar9-Apr-09 4:13 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 0:52
dojohansen1-Jul-09 0:52 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 3:52
dojohansen1-Jul-09 3:52 
GeneralRe: Assignability of output parameters in C# Pin
S. Senthil Kumar1-Jul-09 5:55
S. Senthil Kumar1-Jul-09 5:55 
dojohansen wrote:
and it would not compile if you did the same assignment using return values instead of output parameters.


It would compile if you cast the return value to SomeA

SomeA a = (SomeA) Method2();



dojohansen wrote:
But in my original code the assignment I'm trying to do is known to be safe, as you can verify since the same assignment done from a return value rather than an out parameter builds just fine (and indeed runs fine).


I guess I used the wrong example - here's what you are trying to do

interface ISome {}
class SomeA : ISome {}
class SomeB : ISome {}

void Method()
{
   ISome some = SomeB();
   Method2(out some);
} 

void Method2(out SomeA some) // ISome is NOT SomeA
{
   some = new SomeA();
}


The problem here is ISome need not refer to SomeA, it could refer to any class that implements ISome, but Method2 is expecting SomeA. In fact, the code wouldn't compile even if you removed the out keyword.

The code would compile if you changed the parameter of Method2 to out ISome some instead.
Casting to SomeA when calling Method2 would also work, but not if the parameter is out. The variable you're passing as out needs to be assignable, and the cast expression is not assignable.

And about it working for return types, welcome to the world of covariance and contravariance[^]. Return values are covariant, method parameters are contravariant, which means that return values could be more derived than what the caller expects, whereas method parameters could only be equal or less derived than the caller's parameters.

Regards
Senthil
_____________________________
My Home Page |My Blog | My Articles | My Flickr | WinMacro

GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 6:05
dojohansen1-Jul-09 6:05 
GeneralRe: Assignability of output parameters in C# Pin
S. Senthil Kumar1-Jul-09 18:04
S. Senthil Kumar1-Jul-09 18:04 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 22:25
dojohansen1-Jul-09 22:25 
GeneralRe: Assignability of output parameters in C# Pin
S. Senthil Kumar1-Jul-09 23:02
S. Senthil Kumar1-Jul-09 23:02 
GeneralDiscussion : Can we say Overloading as polymorphism Pin
Cracked-Down8-Apr-09 22:19
Cracked-Down8-Apr-09 22:19 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Nagy Vilmos8-Apr-09 22:49
professionalNagy Vilmos8-Apr-09 22:49 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
dojohansen8-Apr-09 23:31
dojohansen8-Apr-09 23:31 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Nagy Vilmos9-Apr-09 1:46
professionalNagy Vilmos9-Apr-09 1:46 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
dojohansen9-Apr-09 2:46
dojohansen9-Apr-09 2:46 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Rob Philpott8-Apr-09 22:55
Rob Philpott8-Apr-09 22:55 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Cracked-Down8-Apr-09 23:20
Cracked-Down8-Apr-09 23:20 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
dojohansen9-Apr-09 3:04
dojohansen9-Apr-09 3:04 
QuestionString manipulation with large strings [modified] Pin
Harvey Saayman8-Apr-09 22:17
Harvey Saayman8-Apr-09 22:17 
AnswerRe: String manipulation with large strings Pin
Rob Philpott8-Apr-09 22:52
Rob Philpott8-Apr-09 22:52 
GeneralRe: String manipulation with large strings Pin
Harvey Saayman8-Apr-09 23:13
Harvey Saayman8-Apr-09 23:13 
GeneralRe: String manipulation with large strings Pin
Rob Philpott8-Apr-09 23:24
Rob Philpott8-Apr-09 23:24 
GeneralRe: String manipulation with large strings Pin
Harvey Saayman8-Apr-09 23:38
Harvey Saayman8-Apr-09 23:38 

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.