|
One reason might be because "ABC" is interned by the framework. The JIT may do this to save memory, at the cost of getting bitten if you're relying on different strings for multithreading or native interop purposes.
Also, the compiler might be generating code like this:
string __val = "ABC";
string s1 = __val;
string s2 = __val;
The only way to tell for sure is use ILDasm or Reflector to disassemble your code and check out what's going on.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Connor's Christmas Spectacular!
Judah Himango
|
|
|
|
|
Judah Himango wrote: The JIT may do this to save memory
Eventhough this seems like a large overhead as the compiler/runtime would have to check if the string is loaded or not, I'd have expected some documentation for this in the string class.
Judah Himango wrote: ILDasm or Reflector to disassemble your code and check out what's going on.
Doesnt seem to be the case.
And if that were the case, compiler optimization would not even perform the equality check. It'd simply just return 'true'.
- Malhar
|
|
|
|
|
I'd check if the string is interned. My guess is it is interned. You can check using string.IsInterned, which will return null if it is not interned (why the framework designers decided to return a string rather than a boolean is beyond me :P)
You might want to read this decent article on .NET strings[^], might be worth your time.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Connor's Christmas Spectacular!
Judah Himango
|
|
|
|
|
This is from ildasm.. we can see "ABC" is inserted twice
IL_0000: ldstr "ABC"<br />
IL_0005: stloc.0<br />
IL_0006: ldstr "ABC"<br />
IL_000b: stloc.1<br />
IL_000c: ldloc.0<br />
IL_000d: ldloc.1<br />
IL_000e: call bool [mscorlib]System.Object::ReferenceEquals(object, object)<br />
IL_0013: stloc.2<br />
IL_0014: br.s IL_0016<br />
|
|
|
|
|
|
|
|
|
I agree with you that it is because of string interning. However, in this case, there is no way to distinguish if the two objects are different or not. So for the string class, the Equals and ReferenceEquals have no differentiation -- this is little bit frustrating!!
- Malhar
|
|
|
|
|
You can clone a string to get a different instance with the same value.
Or if you use Substring, you will also get a different instance of the same string:
object.ReferenceEquals("Hello", "Hello World".Substring(0,5)) will return false!
Why is this frustrating? The runtime creates interned string instances when the method is JITted. This is much faster than creating the string instance everytime the method in run. If you want a new string instance every time, well, then you still can use Clone().
|
|
|
|
|
Its just a compiler optimization, called constant folding. From the IL you can clearly see the same string reference has been assigned from the string table.
<FONT color=DarkBlue>.method</FONT> <FONT color=DarkGreen></FONT> <FONT color=Blue>private hidebysig static</FONT>
<FONT color=Blue>void</FONT> Main(<FONT color=Blue>string</FONT><FONT color=DarkBlue>[]</FONT> args) <FONT color=Blue>cil managed</FONT>
<FONT color=DarkGreen>
{
<FONT color=DarkBlue>.entrypoint</FONT>
<FONT color=DarkGreen>
<FONT color=DarkGreen>
<FONT color=DarkBlue>.maxstack</FONT> <FONT color=Red>2</FONT>
<FONT color=DarkBlue>.locals</FONT> <FONT color=DarkGreen></FONT> <FONT color=Blue>init</FONT> (<FONT color=Blue>string</FONT> V_0<FONT color=DarkBlue>,</FONT>
<FONT color=Blue>string</FONT> V_1)
<FONT color=DimGray>IL_0000:</FONT> <FONT color=DarkGreen></FONT> <FONT color=DeepPink>ldstr</FONT> <FONT color=Maroon>"ABC"</FONT> <FONT color=DarkGreen></FONT>
<FONT color=DimGray>IL_0005:</FONT> <FONT color=DarkGreen></FONT> <FONT color=DeepPink>stloc.0</FONT>
<FONT color=DimGray>IL_0006:</FONT> <FONT color=DarkGreen></FONT> <FONT color=DeepPink>ldstr</FONT> <FONT color=Maroon>"ABC"</FONT> <FONT color=DarkGreen></FONT>
<FONT color=DimGray>IL_000b:</FONT> <FONT color=DarkGreen></FONT> <FONT color=DeepPink>stloc.1</FONT>
<FONT color=DimGray>IL_000c:</FONT> <FONT color=DarkGreen></FONT> <FONT color=DeepPink>ldloc.0</FONT>
<FONT color=DimGray>IL_000d:</FONT> <FONT color=DarkGreen></FONT> <FONT color=DeepPink>ldloc.1</FONT>
<FONT color=DimGray>IL_000e:</FONT> <FONT color=DarkGreen></FONT> <FONT color=DeepPink>call</FONT> <FONT color=Blue>bool</FONT> <FONT color=DarkBlue>[</FONT>mscorlib<FONT color=DarkGreen></FONT><FONT color=DarkBlue>]</FONT>System.Object<FONT color=DarkGreen></FONT>::ReferenceEquals(<FONT color=Blue>object</FONT><FONT color=DarkBlue>,</FONT>
<FONT color=Blue>object</FONT>) <FONT color=DarkGreen></FONT>
<FONT color=DimGray>IL_0013:</FONT> <FONT color=DarkGreen></FONT> <FONT color=DeepPink>call</FONT> <FONT color=Blue>void</FONT> <FONT color=DarkBlue>[</FONT>mscorlib<FONT color=DarkGreen></FONT><FONT color=DarkBlue>]</FONT>System.Console<FONT color=DarkGreen></FONT>::WriteLine(<FONT color=Blue>bool</FONT>) <FONT color=DarkGreen></FONT>
<FONT color=DimGray>IL_0018:</FONT> <FONT color=DarkGreen></FONT> <FONT color=DeepPink>call</FONT> <FONT color=Blue>string</FONT> <FONT color=DarkBlue>[</FONT>mscorlib<FONT color=DarkGreen></FONT><FONT color=DarkBlue>]</FONT>System.Console<FONT color=DarkGreen></FONT>::ReadLine() <FONT color=DarkGreen></FONT>
<FONT color=DimGray>IL_001d:</FONT> <FONT color=DarkGreen></FONT> <FONT color=DeepPink>pop</FONT>
<FONT color=DimGray>IL_001e:</FONT> <FONT color=DarkGreen></FONT> <FONT color=DeepPink>ret</FONT>
} <FONT color=DarkGreen>
xacc.ide-0.1.2.6 New syntax highlight screenshots of xacc.ide
|
|
|
|
|
I'm written a crazy simple service in C# and a simple class library that exposes 1 class. We're talking "hello world" simple here.
Here's the wierdness.
I have a reference to the class library from the service, when I attempt to start the service, I get an event log entry saying that it can't find the class library.
Event log record-->
Service cannot be started. System.IO.FileNotFoundException: Could not load file or assembly 'SampleClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'SampleClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
at RemotingTest.TestService.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
What I've tried.
Create a console app using the same code -> Works
Remove the calls to the class library and start the service -> Works
uninstall the service/reinstall the service and start the service -> Fails.
Copy the class library dll to the windows system32 directory -> Fails.
????
Any ideas? I'm sure that it is something really stupid, but I can't think of it.
|
|
|
|
|
I suspect it has to do with security rights.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
|
I forgot all about Filemon...it showed me the error in my ways...., the service was looking for the class library in the obj\debug\ folder instead of the bin\debug folder which is where I put the dll. I still don't know why it didn't work when I put it in the system32 folder.
but at least I'm up and running....for now. <hehe>
thanks all.
|
|
|
|
|
I have problems passing parameters from C# application to XSL file and doing a transformation.
My code is below.:
XsltArgumentList xslArg = new XsltArgumentList();
String key = "grandpa/father/son"; // This is the name of node I want pass and gets it value
xslArg.AddParam("xslParam", "", key);
// Create the XslTransform and load the stylesheet.
XslTransform xslt = new XslTransform();
xslt.Load(stylesheet); // xsl stylesheet
XmlUrlResolver xmlUrlResolver = new XmlUrlResolver();
// Output to console.
XmlTextWriter writer = new XmlTextWriter(Console.Out);
// Transform the xml file
xslt.Transform(new XPathDocument(filename), xslArg, writer, xmlUrlResolver);
writer.Close();
----------------
XSL file looks like this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="xslParam"/>
<xsl:template match="/">
<alku><xsl:value-of select="$xslParam"/></alku>
</xsl:template>
</xsl:stylesheet>
The problem is that node grandpa/father/son is not executed! It's passed to XSL file
but XslTransform classes transsform method does not fetch it's value from xml file.
It only shows the key value in console. Node grandpa/father/son has value in it's
xml file.
So, am I missing something or is there a bug in XsltArgumentList and it's transformation.
Has anyone any ideas what could be wrong?
Cheers!
|
|
|
|
|
|
I have a link on my webpage, when clicked upon it takes the user to a page where the website's home page is added to the favourites. Can someone please tell me where to go and look for this on MSDN. I would like to do it through C#. And also, maybe create a directory in My Favourites, and save the link in this directory.
Regards
ma se
|
|
|
|
|
hi, how can i create a nice look status bar in c# 2005?
how i can change look status bar?
Thx for your help.
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
Athlon 64 3500+, Gigabyte GA-K8NXP-9, 2x512 DDR400 Kingston, Inno3d 6200 PCIe, SS2 B2C2 v2.6, HDD := 450gb, Genius 56k int, NEC DVD-RW и мать его Суньмастер 550.
|
|
|
|
|
|
ok thanks.
realy nice
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
Athlon 64 3500+, Gigabyte GA-K8NXP-9, 2x512 DDR400 Kingston, Inno3d 6200 PCIe, SS2 B2C2 v2.6, HDD := 450gb, Genius 56k int, NEC DVD-RW и мать его Суньмастер 550.
|
|
|
|
|
Hi,
I've got a problem I can't solve, maybe I did the wrong searches on google for that !
I have some short and int values I want to put one behind the other in an existing byte array. In C++ i would have done a memcopy for each value passing the pointer to the byte array position I needed. Then, for reading, I would have done something like
int i = (int)b[3];
Now I have to do that in C#, and if possible using a way that would be as fast as the C++ way ... How can I do that ?
Thanks !
Stéphane
|
|
|
|
|
|
Hi there..
I wish to create a mutiview control and then want to dynamically add view cotrols to it while the webpage is displayed. The problem is that when I do this and run my webform, I get the following error:
ActiveViewIndex is being set to '0'. It must be smaller than the current number of View controls '0'. For dynamically added views, make sure they are added before or in Page_PreInit event.
I did create the view controls in the Page_Load() function and then added them to the Views property of the multiview Cotrol object..
How can I fix this...
Thank u!!
|
|
|
|
|
I have an sql table which one of its column data type is bit
How can I update it from c# app ?
Thanks
|
|
|
|