473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why isn't implicit casting allowed on OUT parameters?

A question to the C# language experts: Why isn't this code valid?

static void Foo( out string s )
{
s = "test";
}

static void Main( )
{
object s; // *** Accept any out type!
Foo( out s );
}

If s is uninitialized at the beginning of Foo, what difference does it
make what type the out value will eventually be assigned to, when the
function finally exits?

Thanks!

Nov 17 '05 #1
14 5125
Hi Yurik,

The point here is, you cannot pass object into the method that explicity
expects string.
--

SevDer
http://www.sevder.com
A new .NET Source For .NET Developers
"Yurik" <za*****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
A question to the C# language experts: Why isn't this code valid?

static void Foo( out string s )
{
s = "test";
}

static void Main( )
{
object s; // *** Accept any out type!
Foo( out s );
}

If s is uninitialized at the beginning of Foo, what difference does it
make what type the out value will eventually be assigned to, when the
function finally exits?

Thanks!

Nov 17 '05 #2
Hi Yurik,

I am not a language expert, but I think it has to do with the fact that
the compiler needs to know if the output parameter is a value
(stack-based) type, or a reference (heap-based) type.

This is from the C# specification:

<quote>
1. An output parameter does not create a new storage location. 2
Instead, an output parameter represents the same storage location as the
variable given as the argument in the function member invocation. 3
Thus, the value of an output parameter is always the same as the
underlying variable.
</quote>

If you declare your output parameter as an "object" type, the compiler
does not have enough information on the storage semantics of the parameter.

Regards,

Bennie Haelen

Yurik wrote:
A question to the C# language experts: Why isn't this code valid?

static void Foo( out string s )
{
s = "test";
}

static void Main( )
{
object s; // *** Accept any out type!
Foo( out s );
}

If s is uninitialized at the beginning of Foo, what difference does it
make what type the out value will eventually be assigned to, when the
function finally exits?

Thanks!

Nov 17 '05 #3
Hi Yurik,

I am not a language expert, but I think it has to do with the fact that
the compiler needs to know if the output parameter is a value
(stack-based) type, or a reference (heap-based) type.

This is from the C# specification:

<quote>
1. An output parameter does not create a new storage location. 2
Instead, an output parameter represents the same storage location as the
variable given as the argument in the function member invocation. 3
Thus, the value of an output parameter is always the same as the
underlying variable.
</quote>

If you declare your output parameter as an "object" type, the compiler
does not have enough information on the storage semantics of the parameter.

Regards,

Bennie Haelen

Yurik wrote:
A question to the C# language experts: Why isn't this code valid?

static void Foo( out string s )
{
s = "test";
}

static void Main( )
{
object s; // *** Accept any out type!
Foo( out s );
}

If s is uninitialized at the beginning of Foo, what difference does it
make what type the out value will eventually be assigned to, when the
function finally exits?

Thanks!

Nov 17 '05 #4
SevDer <se****@newsgroup.nospam> wrote:
The point here is, you cannot pass object into the method that explicity
expects string.


But no data is actually being passed in - just the address of the
variable, effectively. If C# were the only language in the framework, I
don't think this would be a problem...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
SevDer <se****@newsgroup.nospam> wrote:
The point here is, you cannot pass object into the method that explicity
expects string.


But no data is actually being passed in - just the address of the
variable, effectively. If C# were the only language in the framework, I
don't think this would be a problem...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Yurik <za*****@gmail.com> wrote:
A question to the C# language experts: Why isn't this code valid?

static void Foo( out string s )
{
s = "test";
}

static void Main( )
{
object s; // *** Accept any out type!
Foo( out s );
}

If s is uninitialized at the beginning of Foo, what difference does it
make what type the out value will eventually be assigned to, when the
function finally exits?


I *suspect* the problem is that not all languages actually support
"out" in the same way - it's possible that some *will* use the value of
the variable before assigning to it. I know from my work on
EasyMock.NET that the current value of a variable which is passed as an
out parameter *is* actually available at run time, if you're using
RealProxy. I suspect that to get type safety absolutely secure, it's
easier to assert that the type is exactly right in C#.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
Yurik <za*****@gmail.com> wrote:
A question to the C# language experts: Why isn't this code valid?

static void Foo( out string s )
{
s = "test";
}

static void Main( )
{
object s; // *** Accept any out type!
Foo( out s );
}

If s is uninitialized at the beginning of Foo, what difference does it
make what type the out value will eventually be assigned to, when the
function finally exits?


I *suspect* the problem is that not all languages actually support
"out" in the same way - it's possible that some *will* use the value of
the variable before assigning to it. I know from my work on
EasyMock.NET that the current value of a variable which is passed as an
out parameter *is* actually available at run time, if you're using
RealProxy. I suspect that to get type safety absolutely secure, it's
easier to assert that the type is exactly right in C#.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8


Jon Skeet [C# MVP] wrote:
The point here is, you cannot pass object into the method that explicity
expects string.
But no data is actually being passed in - just the address of the
variable, effectively. If C# were the only language in the framework, I
don't think this would be a problem...


Isn't there is still a problem with structs (by-value), the caller would
need to allocate the correct amount of space for the struct, or shall
the code that does assignment to structs as out parameters do checking
of the "dynamic-type" of the out parameter and boxing when required?

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #9


Jon Skeet [C# MVP] wrote:
The point here is, you cannot pass object into the method that explicity
expects string.
But no data is actually being passed in - just the address of the
variable, effectively. If C# were the only language in the framework, I
don't think this would be a problem...


Isn't there is still a problem with structs (by-value), the caller would
need to allocate the correct amount of space for the struct, or shall
the code that does assignment to structs as out parameters do checking
of the "dynamic-type" of the out parameter and boxing when required?

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #10
Helge Jensen <he**********@slog.dk> wrote:
Jon Skeet [C# MVP] wrote:
The point here is, you cannot pass object into the method that explicity
expects string.

But no data is actually being passed in - just the address of the
variable, effectively. If C# were the only language in the framework, I
don't think this would be a problem...


Isn't there is still a problem with structs (by-value), the caller would
need to allocate the correct amount of space for the struct, or shall
the code that does assignment to structs as out parameters do checking
of the "dynamic-type" of the out parameter and boxing when required?


Yes, it wouldn't work for value types - but it certainly would work in
the case presented in this thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11
Helge Jensen <he**********@slog.dk> wrote:
Jon Skeet [C# MVP] wrote:
The point here is, you cannot pass object into the method that explicity
expects string.

But no data is actually being passed in - just the address of the
variable, effectively. If C# were the only language in the framework, I
don't think this would be a problem...


Isn't there is still a problem with structs (by-value), the caller would
need to allocate the correct amount of space for the struct, or shall
the code that does assignment to structs as out parameters do checking
of the "dynamic-type" of the out parameter and boxing when required?


Yes, it wouldn't work for value types - but it certainly would work in
the case presented in this thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #12
I still fail to see any major distinction between an out parameter and
a method return type. Isn't out param simply an additional return
value? In which case, why should the callee worry about the type the
return value will be assigned to? The caller should do all the
appropriate casting (IMHO).

Is OUT parameter supported on the C# or on IL level?

Thanks!

Nov 17 '05 #13

"Yurik" <za*****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I still fail to see any major distinction between an out parameter and
a method return type. Isn't out param simply an additional return
value? In which case, why should the callee worry about the type the
return value will be assigned to? The caller should do all the
appropriate casting (IMHO).

Is OUT parameter supported on the C# or on IL level?


A bit of both, actually. The runtime defines the out parameter flag, but
does not enforce semantics. The particluar semantics are up to the language.
Nov 17 '05 #14
Yurik <za*****@gmail.com> wrote:
I still fail to see any major distinction between an out parameter and
a method return type. Isn't out param simply an additional return
value? In which case, why should the callee worry about the type the
return value will be assigned to? The caller should do all the
appropriate casting (IMHO).

Is OUT parameter supported on the C# or on IL level?


I've worked out one potential reason why it has to be the same type.
Consider the following situation:

using System;
using System.Threading;

public class Test
{
object member;

void Foo(out object o)
{
o = "hello";

Thread.Sleep(1000);

Console.WriteLine (o);
}

void SetMemberToThis()
{
Thread.Sleep(500);
member = this;
}

Test()
{
new Thread (new ThreadStart(SetMemberToThis)).Start();
Foo(out member);
}

static void Main()
{
new Test();
}
}
That's fine, but imagine if you changed Foo to be:

void Foo(out string o)
{
o = "hello";

Thread.Sleep(1000);

string x = o;
}

What value would x and o have? Both would be non-strings by the end of
the method...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Simon Ford | last post by:
Hi All, I'm having trouble understanding exactly how I can do some specific implicit casting. There are two problems here; does anyone know what I should be doing? //---------- // (1)...
5
by: Amit | last post by:
I am facing a problem while using SQL Server with VB application. Implicit conversion from datatype text to nvarchar is not allowed. Use the convert function to run this query. When i see the...
9
by: Simon | last post by:
Hi All, Is it possible to disallow implicit casting for an operand of a function written in C? i.e. void foo(int a) {..} short b; foo(b) // error without explicit cast
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
6
by: Gecko | last post by:
I would like to know if there is a way to stop the runtime from implicitly casting values. For exampel, I would like the following code to crash: byte someByte = 5; int someInt = someByte; I...
1
by: Yurik | last post by:
A question to the C# language experts: Why isn't this code valid? static void Foo( out string s ) { s = "test"; } static void Main( ) { object s; // *** Accept any out type!
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.