Solved

C# question regarding variable declaration

Posted on 2011-09-20
7
410 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
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now