Solved

C# question regarding variable declaration

Posted on 2011-09-20
7
418 Views
Last Modified: 2013-12-17
C# Questions:

1. Why am I forced to declarate a string like this:

string str;

2. Why do I get an error when I try to use the new operator like this with a string?:

string str = new string();

3. How come it is allowed to declarate an int like this but not a string:

int i = new int();

4. Why are we allowed to declarate an int or a string without using the NEW operator?
0
Comment
Question by:databoks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 36565619
Using the new keyword when declaring a variable refers to initialize the variable value. If you declare a variable without using new you are only declaring, but not intializing, the variable.

When you use new, you initialize the variable to its initial value. For that, you must use one of the class constructors. Each class (string, int, and any other classes) has its own constructors.

The string class does not allow empty (without parameters) constructors; that's why you can't do:
string str = new string() //this is an empty constructor (without parameters), don't implemented in the string class

Hope that helps.
0
 
LVL 10

Expert Comment

by:oxyoo
ID: 36565647
To append to @RolandDeschain answer, also note that when only declaring a variable some types (value types) get a default value, 0 for an int. whereas string is not a value type and does not get a default value  and instead contains null.

More on default values: http://msdn.microsoft.com/en-us/library/83fhsxwc(v=vs.80).aspx


Good Luck!

0
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36565691
0
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36565693
0
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36565793
1 & 2) string is a C# specific short-hand notation that maps to System.String class. System.String class defines bunch of parameterized constructors. It has no default contructor. So we cannot write "new string()".
3) int is a C# specific short-hand notation that maps to system-defined type System.Int32 struct. This struct defines no contructor and hence we can use its default contructor for instantiation.
4) Possibility to declare int, string and other value types without new is I think a language level facility for clean syntax when using such commonly used types.
0
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 36928758
@databoks

you can declare Ineger Variable like this as per your question

Int32 i = new Int32();  (First "I" Letter is Capital)

String str = new String('a',5);  (First "S" Letter is Capital)
0
 
LVL 8

Author Comment

by:databoks
ID: 37012453
anyone on this?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question