Conditional attribute no longer allows out parameters?

G

GlennDoten

Upgraded to .NET 2.0 and VS.NET'05 and while compiling code that was
fine under 1.1/2003 I'm getting a fatal compiler error:

Conditional member 'GetTraceMode(out System.Web.TraceMode)' cannot have
an out parameter

and here's the method:

[Conditional("TRACE")]
public static void GetTraceMode(out TraceMode mode)
{
mode = TraceMode.Default;
HttpContext context = HttpContext.Current;
if (context != null)
mode = context.Trace.TraceMode;
}

I have to assume this is by design in C# 2.0, but what's the rationale?
I can't find anything about this in the C# language reference.

Thanks.

-glenn-
 
D

Daniel O'Connell [C# MVP]

GlennDoten said:
Upgraded to .NET 2.0 and VS.NET'05 and while compiling code that was
fine under 1.1/2003 I'm getting a fatal compiler error:

Conditional member 'GetTraceMode(out System.Web.TraceMode)' cannot have
an out parameter

and here's the method:

[Conditional("TRACE")]
public static void GetTraceMode(out TraceMode mode)
{
mode = TraceMode.Default;
HttpContext context = HttpContext.Current;
if (context != null)
mode = context.Trace.TraceMode;
}

I have to assume this is by design in C# 2.0, but what's the rationale?
I can't find anything about this in the C# language reference.

The rationale is pretty simple. An out parameter affects a variable in the
calling method, thus the method affects your methods state and when its not
compiled in on a build changes the way the method works. You'll note that
return values were not allowed on 1.1. I'd guess out parameters were a bit
of an oversight in the first spec.
 
G

GlennDoten

Yeah, that makes sense. I suppose I was getting around the requirement
that a conditional method always return void by using the out
parameter. You're guess that allowing out parameters previous to 2.0
was an oversight makes sense to me.

-glenn-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top