Groups | Blog | Home
all groups > c# > september 2003 >

c# : declaring a viarable


Robert Blackwell
9/5/2003 10:52:53 PM
If you define a variable when you declare it, does that mean that you can
never change the variable?
int myVar = 5;

Where as maybe
int myVar;
myVar = 5;

and later on somewhere I could then say
myVar =6;
?

Does that make sense.

Robert Blackwell
9/6/2003 12:43:23 AM
Thanks

Steven Wood
9/6/2003 7:34:09 AM
Robert,

no, you are free to change the variable no matter how you declare it.

Only a variable declared as a const cannot be changed, e.g.

const int myVar = 5; // this cannot be changed

Also, have a look at the keyword 'static' in the documentation.

Steven.

---
Steven Wood

[quoted text, click to view]

Michael Mayer
9/6/2003 8:48:28 PM

[quoted text, click to view]

Steven, I'm sure you know what static means... but mentioning it here could
be confusing.

For Robert, static has nothing to do with being able to change or not change
a variable. It simply means that a variable (or method or whatever element
it modifies) belongs to a particular instance of a class or is shared
between all objects. Sometimes I think the word "shared" in VB.NET makes
more sense.

But since static is sometimes confused with const by beginners, it is a good
idea to look up both and make sure you understand these keywords as you're
learning the language. I'd suggest readonly too, while you are at it.

Here's the full list of keywords for C#:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfCSharpKeywords_PG.asp


--
Mike Mayer
http://www.mag37.com/csharp/
mike@mag37.com


AddThis Social Bookmark Button