Variable Declaration

  • Thread starter Thread starter Vasanth TT
  • Start date Start date
V

Vasanth TT

Guys,

Please help me by answering my questions. Thanks in advance

1. What is the difference between below two declaration statements

int i; i=20;
and
int i=new int(); i=20;


2. String is a class. But why its not declared using "new" keyword?

-Vasanth TT
 
1)

the first one:
declares an int
set to a literal 20

the second one,
declares an int
set to a new int instance of zero
set to literal 20

2) you are confusing declaration with assignment. declaration defines a
variable, and allocates storage for the variable.

the new keywork is used to create a class instance and create storage
for the instance. the new instance be be assigned to a variable. c#
allows this done in one statement.

int i; // i = undefined
int i = new int(); // i = 0 - default value
int i = 20; // i = 20

literals create objects on your behalf:

string s = "hello"; // set s to poit to a readonly string instance
created by compiler.


note: actually an int is structures not a class and can be boxed or
unboxed. an unboxed int is not an object.



-- bruce (sqlwork.com)
 
Back
Top