473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

do you have to always instantiate variables in C#?

TS
I am coming from vb.net. I have seen that most variables need to be
instantiated before using in c#, unlike vb.net. When you declare a variable
and immediately set it's value, does that instantiate it? Since I never call
the constructor (new xxx), I don't know that it does. Ex: int myInt = 5;

thanks
Nov 16 '05 #1
3 1438
TS,

It depends on the type you are using. If your variable is for a
reference type (doesn't derive from System.ValueType), then by default, the
variable is null. You will have to assign something to it in order for it
to not be null.

For value types, by default, the variable is set to the value of the
structure with all the bits set to zero. For numeric types, this amounts to
a value of zero.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TS" <ma*********@311.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I am coming from vb.net. I have seen that most variables need to be
instantiated before using in c#, unlike vb.net. When you declare a variable and immediately set it's value, does that instantiate it? Since I never call the constructor (new xxx), I don't know that it does. Ex: int myInt = 5;

thanks

Nov 16 '05 #2
AFAIK there's no difference between VB and c#: Initialize reference types
(as they refer to nothing, that is, null initially), initialize local value
types before use (they contain garbage before use, but "int myInt = 5;"
initializes an the variable); The compiler will automatically initialize
value-type member variables to default values (=0).

Niki

"TS" <ma*********@311.com> wrote in
news:%2****************@TK2MSFTNGP10.phx.gbl...
I am coming from vb.net. I have seen that most variables need to be
instantiated before using in c#, unlike vb.net. When you declare a variable and immediately set it's value, does that instantiate it? Since I never call the constructor (new xxx), I don't know that it does. Ex: int myInt = 5;

thanks

Nov 16 '05 #3
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
It depends on the type you are using. If your variable is for a
reference type (doesn't derive from System.ValueType), then by default, the
variable is null. You will have to assign something to it in order for it
to not be null.

For value types, by default, the variable is set to the value of the
structure with all the bits set to zero. For numeric types, this amounts to
a value of zero.


Note that the above only applies to instance and static variables.
Local variables aren't set to anything by default, and they *must* be
definitely assigned before their first use.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sarah | last post by:
Hi all. I have a form, and several text and image links on it that should submit the form with different actions. I prepared a simple page with just the code that's not working. PROBLEM:...
31
by: somebody | last post by:
No, I'm not a troll, just angry. I just started learning C++, and find it extremely lacking compared to Java. For example, I had to write my own functions to do something as simple as a case...
1
by: Tony Johansson | last post by:
Hello experts! As this program is now it's works perfectly when running as a application or as an Applet. Now to my question if I just change this row "public class Converter extends Applet "...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
4
by: A. Gonzalez | last post by:
Hi everyone, Does anyone know how to instantiate, or create an XML document file (either using DOM or Readers) from an XML schema (xsd) file? I'm trying to develop an application that can...
8
by: Andrew | last post by:
Hi, friends, I am programming a Windows Form app using VC#. I need to have some info shared through the whole app, e.g., login user id/name. I know I can create a classs to hold these info in...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
2
by: Pyenos | last post by:
class model:pass class view: model() class controller: model() I can instantiate clsss model from inside class view but I can't instantiate class model from inside controller, due to the...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.