By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,437 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

"Conditional" attribute confusion

P: n/a
I use VS2005 with framework 2.0 and I just found a behavior I consider odd.
Here is the code that illustrates th eproblem:

[Conditional("DEBUG")]
public static void MethodA()
{
MethodB()
}

#if DEBUG
public static void MethodB()
{
....
}
#endif

my expectation was that this code will compile perfectly when DEBUG
condition is defined as well as when it is undefined. Apparently, that is
not the case. When compiling with "DEBUG" undefined, compiler generates
error: "The name MethodB does not exist in current context."

This is what Microsoft help says about ConditionalAttribute class:
"Indicates to compilers that a method call or attribute should be ignored
unless a specified conditional compilation symbol is defined". The
conclusion from this is that MethodA from my example should not be compiled.
The reality is that the nethod is compiled - otherwise the error would not
have been reported.

Is there something I am missing about "Conditional" attribute and how the
compiler is working or it something .NET framework documentation or/and C#
compiler are missing ?

Thank you.
Nov 16 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Well, 2 minutes after I submitted this post I found the answer to my
question. Certainly it was me missing the point. "Conditional" attribute
informs compiler to ignore the call to the method but not to ignore the
method during the compilation.

My apology for posting something that I should not have.


>I use VS2005 with framework 2.0 and I just found a behavior I consider odd.
Here is the code that illustrates th eproblem:

[Conditional("DEBUG")]
public static void MethodA()
{
MethodB()
}

#if DEBUG
public static void MethodB()
{
...
}
#endif

my expectation was that this code will compile perfectly when DEBUG
condition is defined as well as when it is undefined. Apparently, that is
not the case. When compiling with "DEBUG" undefined, compiler generates
error: "The name MethodB does not exist in current context."

This is what Microsoft help says about ConditionalAttribute class:
"Indicates to compilers that a method call or attribute should be ignored
unless a specified conditional compilation symbol is defined". The
conclusion from this is that MethodA from my example should not be
compiled. The reality is that the nethod is compiled - otherwise the
error would not have been reported.

Is there something I am missing about "Conditional" attribute and how the
compiler is working or it something .NET framework documentation or/and C#
compiler are missing ?

Thank you.


Nov 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.