Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# question regarding variable declaration

Posted on 2011-09-20
7
Medium Priority
?
431 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 2000 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
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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…

722 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