Solved

C# question regarding variable declaration

Posted on 2011-09-20
7
426 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

631 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