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

Check if variable is defined

P: n/a
Hi Group,

How can you check if a variable is defined in C#?

For example, "string s;" would declare an undefined variable. But how can
I check in an "if" statement wether it's defined or not?

Regards,
Wessel
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

Your question is rather about whether a variable has been initialized. If
this is the case, you can check for default values, which are "null" for
reference types and zeroes of all kinds for value types. You won't be able
to determine though at the point of checking whether the variable in
question hasn't been initialized at all or just its value has been set back
to the initial one.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Wessel Troost" <no*****@like.the.sun> wrote in message
news:Xn******************************@207.46.248.1 6...
Hi Group,

How can you check if a variable is defined in C#?

For example, "string s;" would declare an undefined variable. But how can
I check in an "if" statement wether it's defined or not?

Regards,
Wessel


Nov 16 '05 #2

P: n/a
Hi Wessel,

Well, if the string is declared, you could check if(s == null).

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3

P: n/a

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> a ecrit
dans le message de news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi,

Your question is rather about whether a variable has been initialized. If
this is the case, you can check for default values, which are "null" for
reference types and zeroes of all kinds for value types. You won't be able
to determine though at the point of checking whether the variable in
question hasn't been initialized at all or just its value has been set back to the initial one.


Perhaps he was looking for something like #ifdef in C.
But I don't know if it exists in C#.
Nov 16 '05 #4

P: n/a
Wessel Troost <no*****@like.the.sun> wrote:
How can you check if a variable is defined in C#?

For example, "string s;" would declare an undefined variable.
Well, a variable without an assigned value.
But how can I check in an "if" statement wether it's defined or not?


You can't read it if it hasn't been definitely assigned - so the
compiler will tell you, basically.

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

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Wessel Troost <no*****@like.the.sun> wrote:
How can you check if a variable is defined in C#?

For example, "string s;" would declare an undefined variable.


Well, a variable without an assigned value.


Apologies - this is for local variables only. For instance and static
variables, the variable will have the default value for the type (null
in this case). There is no difference between a variable in this state
and one which has had the default value specifically assigned to it.

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

P: n/a
> Your question is rather about whether a variable has been initialized.
If this is the case, you can check for default values, which are
"null" for reference types and zeroes of all kinds for value types.
You won't be able to determine though at the point of checking whether
the variable in question hasn't been initialized at all or just its
value has been set back to the initial one.


That works, thanks for the quick reply!

Side question: how can you asnwer so quickly? Do you get newsgroup posts
delivered by e-mail perhaps?

Greetings,
Wessel
Nov 16 '05 #7

P: n/a
We check regularly to see if there is anything new :)

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.