R
RN1
If I am not mistaken, String is reference type. Can someone please
explain me why is it reference type & not value type?
explain me why is it reference type & not value type?
A string can potentially contain a lot of data, and there's no telling what
size a particular string might need to be.
Therefore, not only does it make little sense to try and store a string ina
register or on the stack, or anything else you'd do with a value type,
strings have always been pointers in traditional languages.
Why would it be a value type?
--
Jonathan Wood
SoftCircuits Programminghttp://www.softcircuits.com
- Show quoted text -
A string can potentially contain a lot of data, and there's no telling
what
size a particular string might need to be.
Therefore, not only does it make little sense to try and store a string in
a
register or on the stack, or anything else you'd do with a value type,
strings have always been pointers in traditional languages.
Why would it be a value type?
--
Jonathan Wood
SoftCircuits Programminghttp://www.softcircuits.com
- Show quoted text -
No, Strings in Pascal (and ADA as I reacll) are tru types.
Don't know about C# but I hope the compiler takes care of this. By
references or not is not of interest. What is of interest is if you have
the option to use call by value for strings or if all parameters are
treated as references. Can you use call by value in C# how do you use call
by value?
Lars,
This appears to be to me but I don't know what "no" refers to, or what "tru
types" are.
I could be wrong but it seems to me you are confusing terms. I'm not that
familiar with Delphi but a reference in C++ is different from a .NET
reference type. I don't know the exact criteria used to determine value or
reference type, but to me a reference type is a pointer, and so any type
that requires a pointer seems it would be a good candidate. And as far as
I'm concerned, all strings require pointers, whether they are made available
to the language that uses them or not. (Certainly, it wouldn't make sense to
ever store a string in a register or even on the stack.)
According tohttp://www.knowdotnet.com/articles/referencetypes2.html, "One
of the most common mistakes developers make when learning .NET is confusing
Reference and Value Types with Passing values by Reference or Value." Again,
I think you are confusing terms.
OK...now suppose I have the following code:
<script runat="server">
Class MyInt
Public MyValue As Integer
End Class
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim x As New MyInt
...................
...................
...................
End Sub
</script>
If I am not wrong, the Dim line in the Page_Load sub "creates space in
the heap & returns a pointer (say, 0x000001)". To whom does the Dim
line return the pointer?
Please correct me if I am wrong.
Lars,
This appears to be to me but I don't know what "no" refers to, or what
"tru
types" are.
I could be wrong but it seems to me you are confusing terms. I'm not that
familiar with Delphi but a reference in C++ is different from a .NET
reference type. I don't know the exact criteria used to determine value or
reference type, but to me a reference type is a pointer, and so any type
that requires a pointer seems it would be a good candidate. And as far as
I'm concerned, all strings require pointers, whether they are made
available
to the language that uses them or not. (Certainly, it wouldn't make sense
to
ever store a string in a register or even on the stack.)
According tohttp://www.knowdotnet.com/articles/referencetypes2.html, "One
of the most common mistakes developers make when learning .NET is
confusing
Reference and Value Types with Passing values by Reference or Value."
Again,
I think you are confusing terms.
bruce barker said:no heap is used.
the variable x is on the stack and is the value, rather than reference to
the value. the variable MyValue being a class static value, is allocated
in the static class definition structure, and again is the value, not a
pointer to the value.
If I am not mistaken, String is reference type. Can someone please
explain me why is it reference type & not value type?
If I am not mistaken, String is reference type. Can someone please
explain me why is it reference type & not value type?
One question: When you send an object as a Call By Value parameter
and the paramenter. Doesn it invoke the Copyconstructor and default
constructors as C++ does?
if you have a string that includes "aao" or "<>". In HTML
these characters are replaced the later with "&kt;>".
When you check the length of suct string what is the result.
How does the String class handle Chinese letters?
RN1 said:OK...now suppose I have the following code:
<script runat="server">
Class MyInt
Public MyValue As Integer
End Class
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim x As New MyInt
...................
...................
...................
End Sub
</script>
If I am not wrong, the Dim line in the Page_Load sub "creates space in
the heap & returns a pointer (say, 0x000001)". To whom does the Dim
line return the pointer?
Please correct me if I am wrong.
So to the original point; is a string a class or a structure? Ans: It's
a
class.
Therefore its always allocated in the heap and a variable of string type
is
always a reference to this string object allocated on the heap.
Dim x As String = "Hello World"
Dim y As String
y = x
As with all classes in this case y and x both reference the same String
object.
What makes this less obvious (as Mykola alluded to) is the immutable
nature
of a String. Once a string object has been allocated and initialised its
content cannot be changed, not even changes that don't involve changing
the
length. This results in code which makes strings look like they are
behaving as structures because no where do see an operation on one
reference
that is visable through another reference.
A similar type to a string is a character array but a character array is
mutable :-
Dim z As Char() = New Char(2) {"A"c, "B"c, "C"c}
Dim x As Integer()
x = z
x(1) = "X"c
Console.WriteLine(z(1))
Note that both z and x reference the same Array object. A modification
made
to the object via one reference is visible using the other. This sort of
operation just isn't available on the string object. Its the lack of this
sort of thing happening in code to a string which gives the illusion of it
being a structure.
However Mykola is in error stating that you can't pass strings as ByRef.
You can (in both VB and C#).
Public Sub DoSomething(ByRef rs As String)
rs = "New content"
End Sub
Dim s as String = "Hello World"
DoSomething(s)
Console.WriteLine(s)
This confusion arises from the overloaded use of the word Reference (which
is why I prefer to think in terms of Structures or Classes). When you
think about it all variables are ultimately references to somewhere in
memory where a value is stored. For example:-
Sub Thing()
Dim i as Integer
Dim o as Object
The symbol i resolves to a offset pointer from the a fixed point on the
stack frame where 4 bytes hold the actual value of the integer. The
symbol
o resolves to an offset pointer (likely the same as for i + 4) from the
same
fixed point on the stack frame which will hold a reference to some
reference
to an object stored on the heap.
Passing ByRef has nothing to do with whether the value is a Structure or a
Class but everything to do with where relative to a fixed point on a stack
frame to find the variable value (be that the actual structure content or
the reference to a heap held object). Since at the time of call the size
of
the current frame is known its relatively simple to supply an offset from
a
fixed point in the new frame back into the previous.
--
Anthony Jones - MVP ASP/ASP.NET
Note 1:-
A couple of other reasons (apart from my primary reason that the word
reference is already over used) I prefer to think in terms of Structure or
Class instead of ValueType or ReferenceType are:-
1). Its possible to have a reference to a structure. I.e. when its boxed
for example referenced via variable typed as Object.
2.) The MustInherit (abstract) type System.ValueType from which all
'ValueTypes' inherit is a class not a structure. How can a structure
inherit
from a Class? Ans: it can't. Only when Boxed does a Structure inherit
from
ValueType and at the point its a reference to copy on the heap.
Its all quite confusing.
Lars said:Hi
Does the String class implements operators to take care of this.
Can you write you own class for example Complex that defines the assignment
operator and comparement operators. Si that if you wrote Z1=Z2 copies the
values Z2.x and Z2.y to Z1 leavind Z2 unchained. Do you have to create a new
object of Z and return for this?
I Think I got it
public void swap( ref String S1, ref String S2 )
{
String tmp1=S1;
S2=S1;
S1=tmp;
}
What this function does is swapping the reference values. Compare it to the
follwing C++ and Delphi code
public void swap( String&* S1, String&* S2 )
{
String* tmp1=S1;
S2=S1;
S1=tmp;
}
When you takea basic course in Datastructures and Algoritms you learn than a
REFERENCE is a reference that refers to another object. Weather it's
implemented by pinters, memory on the heap or any thing else doesn't matter.
A Value is value treated as a value (object). The OBJECT if you prefer.
Every Reference has to refer to an OBJECT. A teacher I had at the Universaty
(Basic Datatypes and Algoritms) told us to look at References is a non
language depandant matter as below. 14 years later I still recall it.
References (pointers for old C programmers, instanses of classes in C#)
{Reference} ----> {Object ( 1,2,3,4,5,6,67,78,)}
or
{Reference} ----> {Object ( 1,2,3,4,5,6,67,78,),Object (
1,2,3,4,5,6,67,78),Object("EHROIERO"}
Objects
{Object ( 1,2,3,4,5,6,67,78,)}
{Object ( 1,2,3,4,5,6,78,)}
{Object ("DJÖLFD","DODHFO")}
For those of you who have hard understanding heap and stack. Look ar the
example above. A pcture says more than a thousand words as they say. If the
object reffered to is on the heap, file, on another server doesn't matter.
Think abstract and you get it.
Not really wonce you know that all instances of classes are references it's
no problem.
What I did was to not only mix C++ and C# I also have a lot of
Delphi code in memmory. In the matter of how to implement instances of
classes Delphi is more like C# than C++ is. In a true Obecj Orienter
Language as Smalltalk every thing is a reference. As I understand C# is move
towards that direction. But C# is not near an truly OBject Oriented
language. Although it you can use OOP techniques. The build in classes oin
C# doesn't support encapsulation fully. For example
// Does LabekX know that you change the value of the Text when I do this?
Does C# allow you to define properties like Delphi.
LabelX.Text = "Some text";
This is important to think of when you wrote your own classes. For every
member (variable) in the class write an accessor and modifier method. In
general Set Get methods. It makes life easy when you inherit the class in
another class.
Lars said:Hi
Check out the page
http://msdn2.microsoft.com/en-us/library/aa691324(VS.71).aspx
Out take from the page
C# Language Specification
7.2.2 Operator overloading
All unary and binary operators have predefined implementations that are
automatically available in any expression. In addition to the predefined
implementations, user-defined implementations can be introduced by including
operator declarations in classes and structs (Section 10.9). User-defined
operator implementations always take precedence over predefined operator
implementations: Only when no applicable user-defined operator
implementations exist will the predefined operator implementations be
considered.
Does this mean that you can override all operators except assignment =
operator.