473,761 Members | 4,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does string not require a new like other reference types?

I can't believe why I had not noticed this before and why I never asked it
before...

When defining a string why is it not required to use the new keyword? Like:

Dim a As String = New String

You can do it when using the string type with some arguments in the
constructor but it look like there is no empty constructor on the String
class.

Seem odd to me that all the other reference types seem to require a new to
create the instance portion of the type and String does not.

Thanks for any insight...
Jun 27 '08
21 1459
I am asking because I wanted to explain the difference between these two
types to someone else and could not come up with a real reason (other than -
it just works that way with strings) for them. Right, I'm not the designer
but that does not stop me form being curious :)

Inconsistencies bug me. It just seemed like something that would pop up in a
Microsoft document somewhere and I have had no luck in locating it. Another
thing that bugs me.

Now that I had the discussion I am ok. Seems more like it was a mixture of
how strings are inherently immutable and how it better to make them work
like ValueTypes in these cases.
"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:1A******** *************** ***********@mic rosoft.com...
Ray,

Your replies sounds to me like somebody driving a car once heard that
there was a carburator in it, and now ask why he cannot find it in his
current car.

It is important for a driver, that a car do exactly as it should do? You
are not the one who designs Net and the way it works.

By the way, playing with words does not work here.

Dim a as integer "results" in a 0
Dim b as string "results" in a ""

Both as with all valuetypes with the lowest value possible.

Strange that you did not mention the Struct DateTime which has almost the
same behaviours

Just my idea reading your replies.



Jun 27 '08 #21
In article <e1************ **@TK2MSFTNGP05 .phx.gbl>,
Ray Cassick <rc******@enter procity.comwrot e:
The storage is declared on the managed stack. IF the type is a ValueType
then nothing else is needed and the storage is all done on the stack. IF the
type is a reference type then the stack storage ends up just being a pointer
that will end up being an address on the heap somewhere but points to
nowhere just yet.
Pretty much, there are exceptions...
When you do this:

Dim a As ClassName = New ClassName...

THEN the storage is allocated on the stack AND the storage is allocated on
the heap and the stack points to the proper location on the heap.

So, I guess my question is.. if String is a reference type why do you NOT
NEED to allocate it using the New keyword every time? Almost seems like
strings are an odd case with respect to this.
Nope.

This is one of the exceptions to the stack/heap storage option. String
constants results in a reference that points to the code. Just as you'd
do if you were programming in assembly and were assigning a pointer to
a string constant.

So, string constants are pointers to code, and the implementation of
the + and & operators create a new reference and then return that.

--
J.B. Moreno
Jun 27 '08 #22

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

Similar topics

30
3476
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then I've also heard there are lots of weird ways to do some things kinda like Perl which is bad for me. Any other ideas?
8
2559
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
8
5043
by: Nader | last post by:
Hello all, In C# string is a reference type but I learned that string is different from other reference types such as class. For example, if you pass a string argument to a method and then change the value in that method the modification will not be visible outside the method. However this is not true for classes. In my example I am not using ref keyword. Thanks for feedback.
6
2822
by: Chris Simmons | last post by:
I know that a String is immutable, but I don't understand why this piece of code fails in nUnit: // BEGIN CODE using System; class Test { public static void Main( String args )
6
1703
by: Bob Day | last post by:
VS 2003 The documentation says " Nothing keyword represents the default value of any data type" this is simply not true and causing a lot of problems. 1) Consider an SQL table of 3 columns: Column1 bit no nulls Column2 string no nulls Column3 DateTime no nulls
0
5115
by: Richard Gregory | last post by:
Hi, I have the wsdl below, for an Axis web service, and when I select Add Web Refernce in Visual Studio the proxy is missing a class representing the returnedElementsType (see reference.cs below the wsdl). This complex type is a collection of another complex type(elementType), and the Reference.cs has an array of these rather than the single returnedElementsType. If If I want to be able to obtain these elements from the SOAP response I...
4
6195
by: haitao.song | last post by:
Hi, As it is always stated that value type is allocated on stack, while reference types are on managed heap. How about the struct with string members? stuct A { string str; } String type is considered as reference type.... My guess is the struct A is actually a valuetype with a string reference/address. So it acts as if struct A { int ptrStr; } Anyway, the ptrStr is like a
4
1726
by: eBob.com | last post by:
In my class which contains the code for my worker thread I have ... Public MustInherit Class Base_Miner #Region " Delegates for accessing main UI form " Delegate Sub DelegAddProgressBar(ByVal uiform As Form1, ByRef si As MTCC02.Form1.SiteRunOpts, _ ByVal numitems As Integer) #End Region
5
4611
by: Anders Borum | last post by:
Hi! While implementing a property manager (that supports key / value pairs), I was wondering how to constrain T to a struct or string type. Basically, I guess what I'm looking for is the common denominator for structs and strings and while looking through the SDK I only noticed the IEquatable<T> interface. So I implemented the class with methods such as the following, but I'm aware that this is not the right approach.
0
10115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.