473,385 Members | 1,396 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,385 software developers and data experts.

HasElementType

Type.HasElementType -
"Gets a value indicating whether the current Type encompasses or refers to
another type; that is, whether the current Type is an array, a pointer, or
is passed by reference." So why does this return false? String is passed
by ref and is a ref type.

string s = "text";
Console.WriteLine("String hasElement:" + s.GetType().HasElementType);

Output:
String hasElement:False

--
William Stacey, MVP

Nov 15 '05 #1
9 1703
William,

In the documentation for the HasElementType property, the meaning of
"reference" is not a manged reference, but rather, a reference in the
classic C++ sense (it refers to getting the type of "Int32&" for a reference
type). If you try your code with the type of Object, it returns false as
well.

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

"William Stacey" <st***********@mvps.org> wrote in message
news:OD**************@tk2msftngp13.phx.gbl...
Type.HasElementType -
"Gets a value indicating whether the current Type encompasses or refers to
another type; that is, whether the current Type is an array, a pointer, or
is passed by reference." So why does this return false? String is passed
by ref and is a ref type.

string s = "text";
Console.WriteLine("String hasElement:" + s.GetType().HasElementType);

Output:
String hasElement:False

--
William Stacey, MVP

Nov 15 '05 #2
"true if the Type is an array, a pointer, or is passed by reference;
otherwise, false."
So below, byte[] is passed by ref, as is int[] and they return true as
expected. Pointer is true as expected. However string is also a ref type
and passed by ref as is object, so why are they false?

public static void Main()
{
unsafe {
Console.WriteLine("byte[].HasElementType:
"+typeof(byte[]).HasElementType);
Console.WriteLine("int[].HasElementType: "+typeof(int[]).HasElementType);
Console.WriteLine("byte*.HasElementType: "+typeof(byte*).HasElementType);
Console.WriteLine("string.HasElementType:
"+typeof(string).HasElementType);
Console.WriteLine("object.HasElementType:
"+typeof(object).HasElementType);
}
}

Output:
byte[].HasElementType: True
int[].HasElementType: True
byte*.HasElementType: True
string.HasElementType: False
object.HasElementType: False

--
William Stacey, MVP

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:#K*************@tk2msftngp13.phx.gbl...
William,

In the documentation for the HasElementType property, the meaning of
"reference" is not a manged reference, but rather, a reference in the
classic C++ sense (it refers to getting the type of "Int32&" for a reference type). If you try your code with the type of Object, it returns false as
well.

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

"William Stacey" <st***********@mvps.org> wrote in message
news:OD**************@tk2msftngp13.phx.gbl...
Type.HasElementType -
"Gets a value indicating whether the current Type encompasses or refers to another type; that is, whether the current Type is an array, a pointer, or is passed by reference." So why does this return false? String is passed by ref and is a ref type.

string s = "text";
Console.WriteLine("String hasElement:" + s.GetType().HasElementType);

Output:
String hasElement:False

--
William Stacey, MVP


Nov 15 '05 #3
William Stacey <st***********@mvps.org> wrote:
"true if the Type is an array, a pointer, or is passed by reference;
otherwise, false."
So below, byte[] is passed by ref, as is int[] and they return true as
expected.
No, byte[] references are passed by value, but they fall into the "if
the Type is an array" clause of the documentation.
Pointer is true as expected. However string is also a ref type
and passed by ref as is object, so why are they false?


They're *not* passed by reference.

See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
> No, byte[] references are passed by value, but they fall into the "if
the Type is an array" clause of the documentation.


The "pointer" to the array is passed by value. However it is still a
reference type. If you use the "ref" keyword to pass it, then your passing
a pointer to a pointer. And the pointer is passed by value. The
distinction is, value types are passed by value on the stack, ref types pass
the 4 bytes pointer by default. It still passing by reference (not to
confuse that with the "ref" keyword).
Pointer is true as expected. However string is also a ref type
and passed by ref as is object, so why are they false?


They're *not* passed by reference.


See above.

--
William Stacey, MVP

Nov 15 '05 #5
Thanks Jon, instead of commenting on all that, here is a document I created
that gets into the detail of this at the memory level. This shows exactly
what gets passed when passing a byte[] by-value and by-reference. There is
also a few other bits that you may find interesting or could answer
(question is noted in the text.) Please feel free to comment. If you can't
view this, please also let me know. Cheers!
http://www.mvptools.com/docs/PassByteArrays.mht

--
William Stacey, MVP
Nov 15 '05 #6
William Stacey <st***********@mvps.org> wrote:
Thanks Jon, instead of commenting on all that, here is a document I created
that gets into the detail of this at the memory level. This shows exactly
what gets passed when passing a byte[] by-value and by-reference. There is
also a few other bits that you may find interesting or could answer
(question is noted in the text.) Please feel free to comment. If you can't
view this, please also let me know. Cheers!
http://www.mvptools.com/docs/PassByteArrays.mht


The problem with looking at it at the implementation level is that
pass-by-reference and pass-by-value aren't *defined* at the
implementation level. They're defined in terms of parameters. When you
talk about the "pointers [to the variables themselves] being passed by
value" in paragraph 3, that doesn't apply to any definition of pass-by-
value I've seen, because the pointers aren't the value of any of the
parameters. The point of describing pass-by-reference and pass-by-value
semantics is to understand how *parameters* are passed.

In the case of pass-by-value, the value of the actual parameter
expression becomes the value of the new local variable (the formal
parameter).

In the case of pass-by-reference, the formal parameter is aliased with
the actual parameter expression (which must therefore be an L-value) so
that any changes in the formal parameter's value are also visible as
changes to the actual parameter expression's value.

The parameters in the call

GetRefs (ba, ba2, ref ba, ref ba2)

are ba, ba2, ba (again), and ba2 (again). The first parameters are
passed by value, the second are passed by reference. There are no other
parameters, so no other things can really be described as being passed,
IMO. The title of your page is misleading in this respect - byte arrays
themselves can never be passed, as there is no expression whose value
is a byte array - only a *reference* to a byte array.

One of the reasons the explanation gets difficult (paragraph 6) is that
the JIT gets involved - and frankly, I don't understand the details of
what the JIT does. That's the good thing about sticking at the language
level - everything can be explained by the language specification.
Obviously we need to have some ideas about performance etc, but in
terms of explaining the way the language behaves, we can stick at the
language specification level.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
> value" in paragraph 3, that doesn't apply to any definition of pass-by-
value I've seen, because the pointers aren't the value of any of the
parameters.
The pointers are the only values that get passed in both cases regarding
reference types. In the (ba, ref ba) example: the first parameter that is
passed is the reference (i.e. pointer) that ba holds. In the second
parameter it is the pointer to ba that gets passed (or reference to ba.) In
both cases, references are passed. That was my point and I think we both
agree.
In the case of pass-by-value, the value of the actual parameter
expression becomes the value of the new local variable (the formal
parameter).
Agreed. I don't think I said otherwise. However there is that exception
where a new local variable address is not created as I showed. I still
wouldn't mind getting some discussion on that from some kind sole.
In the case of pass-by-reference, the formal parameter is aliased with
the actual parameter expression (which must therefore be an L-value) so
that any changes in the formal parameter's value are also visible as
changes to the actual parameter expression's value.
Agreed. I think I showed that in some detail.
IMO. The title of your page is misleading in this respect - byte arrays
themselves can never be passed, as there is no expression whose value
is a byte array - only a *reference* to a byte array.
You are welcome to your opinion. As a byte[] is a reference type, it does
make sense, IMO, to talk about passing reference types by value and by
reference. In fact, there is a help page on it. There is also one titled
"Passing Arrays Using ref and out". I think you are parsing a bit fine here
for some reason. I never said, indirectly or otherwise, that you actually
pass the bytes of the array itself. If nothing else, that is very clear in
the text.
Obviously we need to have some ideas about performance etc, but in
terms of explaining the way the language behaves, we can stick at the
language specification level.


Sure that is fine for many things. Sometimes you need more. It all depends
on what your talking about or what the question is. Cheers.

--
William Stacey, MVP

Nov 15 '05 #8
William Stacey <st***********@mvps.org> wrote:
value" in paragraph 3, that doesn't apply to any definition of pass-by-
value I've seen, because the pointers aren't the value of any of the
parameters.
The pointers are the only values that get passed in both cases regarding
reference types. In the (ba, ref ba) example: the first parameter that is
passed is the reference (i.e. pointer) that ba holds. In the second
parameter it is the pointer to ba that gets passed (or reference to ba.) In
both cases, references are passed. That was my point and I think we both
agree.


Sort of - but not quite. The word "reference" here is problematic here
because it's so overloaded. In strict ways no reference gets passed in
the second case - the *parameter* gets passed *by* reference, and it
being a reference-type variable doesn't actually make any conceptual
difference. What I'm trying to get at is that the value itself is
entirely disregarded (and not even evaluated). Now the fact that it's
probably *implemented* as a a pointer which is like a reference
shouldn't (IMO) come into these discussions, as that's an
implementation detail which should be clearly separated from the
language terminology.
In the case of pass-by-value, the value of the actual parameter
expression becomes the value of the new local variable (the formal
parameter).


Agreed. I don't think I said otherwise.


No, I was just clarifying.
However there is that exception
where a new local variable address is not created as I showed. I still
wouldn't mind getting some discussion on that from some kind sole.
But *conceptually* it still *is* being created. I don't care what the
implementation actually does so long as the end result is the same as
the spec says it will be.
In the case of pass-by-reference, the formal parameter is aliased with
the actual parameter expression (which must therefore be an L-value) so
that any changes in the formal parameter's value are also visible as
changes to the actual parameter expression's value.


Agreed. I think I showed that in some detail.


Again, just clarifying.
IMO. The title of your page is misleading in this respect - byte arrays
themselves can never be passed, as there is no expression whose value
is a byte array - only a *reference* to a byte array.


You are welcome to your opinion. As a byte[] is a reference type, it does
make sense, IMO, to talk about passing reference types by value and by
reference.


It absolutely makes sense to talk about passing reference types by
value and by reference - but passing a byte array reference normally,
it is passed by value, rather than the array being passed by reference
as you originally claimed.
In fact, there is a help page on it. There is also one titled
"Passing Arrays Using ref and out". I think you are parsing a bit fine here
for some reason. I never said, indirectly or otherwise, that you actually
pass the bytes of the array itself.
No, but you said that byte arrays were passed by reference (implying
"by default"), when in fact byte array references are passed by value,
which is a different set of semantics.
If nothing else, that is very clear in the text.
Obviously we need to have some ideas about performance etc, but in
terms of explaining the way the language behaves, we can stick at the
language specification level.


Sure that is fine for many things. Sometimes you need more. It all depends
on what your talking about or what the question is. Cheers.


But the question, as I understand it, is "are byte arrays passed by
reference by default" as that was the thing I disagreed with in your
post much earlier on. Determining the answer to that question doesn't
require anything beyond the language specification and a definition of
what "pass by reference" and "pass by value" each mean.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
Ok. So your saying array are passed by reference?
JK :-)

--
William Stacey, MVP

Nov 15 '05 #10

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

Similar topics

11
by: DraguVaso | last post by:
Hi, I want to make a small application in VB.NET that relinks all the query's and tables in an Access database that are linked via ODBC to an SQL Server. It must be able to relink all the tables...
10
by: Ger | last post by:
I am having problems using VB.Net's Management base object on a machine hosting Windows Server 2003. I am trying to set file permissions from a Windows Service. These files may be loacted on a...
2
by: timnels | last post by:
I've got some code that is trolling through and assembly looking for methods that have parameters whose BaseType is DataSet (they are strongly-typed DataSets). .ParameterType.BaseType is always...
3
by: my cats, Gag and yak | last post by:
the following keeps showing up in Application_Error I have re-installed VS2008 and it still happens I do not know what this means, thank you your help. {Name = "EventArgs" FullName =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.