Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

.net - C# variable initializations vs assignment

In a book I found following (translation):

Initialization means assigning the value of the variable at the declaration time. int X=5 is called an initialization command.

EDIT: It just says that term initialization is used only when you assign value at the declaration time. If you do it later, its just assignement (according to it - I do not think so and that is why I am asking). Is it true or not?

Well, I have always thought (and according to others on the net, too) about initialization in the terms of first assignment of a value to a variable. I think that int X=5 is just assignment as a part of declaration.

I tried to search on MSDN with no luck. Thanks for any information.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If all you are asking about is terminology (*which isn't really clear from your question), "Initialization" of a variable is, literally, the first time a value is assigned to it. This term comes from the fact that you are giving the variable it's "initial" value.

This should (obviously) happen before it is used the first time.

int x=5;

is a declaration and an initialization, and is really just convenient shorthand for

int x;
x=5;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...