Click here to Skip to main content
15,892,005 members
Home / Discussions / C#
   

C#

 
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 
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 
Hi,

Thanks for trying to help out. I think I get it. I disagree with your claim that my code is trying to perform that illegal assignment. "Field" is of the interface type (the less derived type), and the out parameter is of the concrete type that implements the interface type. The only assignment that would ever be attempted if that code ran (forgetting for a second the fact that it does not compile!) is the first of your two assignments, which is indeed legal.

In C# an out parameter cannot be used in the method declaring it unless that method has assigned it; it is considered unassigned, just like any local variable that has been declared but not initialized. Because of this "strictly one-way" behavior, an out parameter *could* logically speaking support the same assignability rules as return values. A ref parameter on the other hand could not, since it can be used in both directions, thus resulting in a downcast (to a more derived type) that is implicit yet unsafe.

But then I thought for a moment about what the compiler would actually do with output and ref parameters, and it occured to me that it probably generates exactly the same IL for both! After all, the only difference is that you're not allowed to use the out parameter in the declaring method, and you are required to always assign it, both of which can be verified by the compiler but result in the same IL code (or no IL code at all if changing a ref parameter to an out parameter results in a build error - changing an out to a ref however will never cause a build error).

I think I've therefore arrived at the following conclusion:

1) There are no reasons at the abstract level why a formal out parameter could not be more derived than it's corresponding actual parameter. If the language allowed callers to use foo(out obj) where obj is declared as "object", no unsafe casting would ever result regardless of how derived the formal parameter is, e.g. foo(out int n) or foo(out CryptoStream s).

2) There are some (probably pretty good) practical reasons why parameters should all follow the same rules. If they do, formal parameters must necessarily be less derived than actual ones.
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 
GeneralRe: String manipulation with large strings Pin
dojohansen8-Apr-09 23:58
dojohansen8-Apr-09 23:58 
GeneralRe: String manipulation with large strings Pin
Harvey Saayman9-Apr-09 0:30
Harvey Saayman9-Apr-09 0:30 
GeneralRe: String manipulation with large strings Pin
dojohansen9-Apr-09 1:14
dojohansen9-Apr-09 1:14 

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.