James Curran said:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:u3Hg#
[email protected]...
Well, since nothing is assigned to it, nor assigned from it, it would
have to be a method in any language, right? ;-)
Not really.
In Delphi it is pretty straight forward as there are language constructs
that shows that a property is mapped to a field.
fCount: Integer;
property Count: integer read fCount;
Hence the compiler simply map the property Count to the virtual address of
fCount.
In managed langauges, as Java and CIL; there is no such thing as a property.
C# have properties as syntactic sugar for getters and setters, but as you
said; CIL only sports methods.
But the Just In Time Compiler (JIT) will optimize and scan all get and set
methods. If the methods is just read and assignment to fields it will be
inlined.
Hence..
private int count;
public int Count
{
get
{
return count;
}
}
.... would never yield a method call to get_Count, but map straight to the
virtual address of count.
This is true for both Java and CIL.
JITters are quite smart. Just look how they manages strings:
int digit = 2;
string strsimple = "1" + digit.ToString() + "3";
C# implies that two calls are made to an overridden + operator method on the
class System.String in conjunction with the ToString() method. Luckely for
us, and our users, this is not what we get, as it would give us the
performance of a snail =)
The JITter could care no less about language constructs in C# and CIL, and
simply calculate the size of the strings and allocate a a new string and
then copy the buffers. No methods called whatsoever. Really!
But what about this?
string str = "Hello, " + name + "!" + " It is " + day + " and the weather is
" + weather + ". Have a nice day!";
In this case, the JITter would actually create a StringBuilder (StringBuffer
in Java).
And you might think it would use be something like
tempstr.Append(...).Append(..).Append(...).ToString();
But No! There is no such thing as an Append method on a StringBuilder when
it comes actual runtime. That method will be replaced by the JITter with
proper cpu-instructions for moving unicode, depending on what cpu you got.
And future computers will have a unicode-chip that does encodings in
hardware. Strings will be faster...
This is what we love about Managed Code. We get all the sugar from C#
syntax, and the JITter can become smarter and smarter, move to better and
better computers, without breaking any code.
What you write in C# is not WYSIWYG but WYSGDSTYT
WYSIWYG - What You See Is What You Get
WYSGDSTYT- What You See Gets Done Smarter Than You Think
Regards
- Michael S