Solved

C# question regarding variable declaration

Posted on 2011-09-20
7
413 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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