使用getstring方法的问题(c#) [英] Problem in using the getstring method (c#)

查看:1016
本文介绍了使用getstring方法的问题(c#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中有下一行。



rdr = scomm.ExecuteReader();

log.Debug(log 1+ rdr.Read()++ rdr.GetString(0)++ String.ReferenceEquals(rdr.GetString(0),SUCCES));



这是日志文件中的返回log 1True SUCCES False。



我不明白为什么String.ReferenceEquals(rdr.GetString(0),SUCCES)不是真的。



提前谢谢!



我的尝试:



我试图验证sql命令执行的结果。

解决方案

参见Object.ReferenceEquals方法(对象,对象)(系统) [ ^ ] 。


还看看这个CodeProject文章:比较.NET中的平等价值:身份和对等 [ ^ ]。

不要做那样的事情!

问题是你不能控制执行顺序:编译器(更糟糕的是,优化器)可以自由评估该字符串的元素按其选择的顺序排列 - 并且它不知道需要首先调用Read方法。

尝试明确订购操作:

 rdr = scomm.ExecuteReader(); 
bool isData = rdr.Read();
string s1 = rdr.GetString( 0 );
string s2 = String .ReferenceEquals(rdr.GetString( 0 ), SUCCES);
log.Debug( log 1 + isData + + s1 + + s2);



至于为什么它们不是同一个引用,字符串的实习池只适用于字符串文字 - 而不是在编译时未定义的字符串。试试这个:

  private   string  GC ( string  s1, string  s2)
{
return s1 + s2;
}
...
string s1 = GC( hello there);
string s2 = GC( hello there);
string s3 = 你好那里< /跨度>;
string s4 = 你好那里< /跨度>;

Console.WriteLine( {0}:{1}, s1 == s2, string .ReferenceEquals(s1,s2));
Console.WriteLine( {0}:{1},s1 == s3 , string .ReferenceEquals(s1,s3));
Console.WriteLine( {0}:{1},s3 == s4 , string .ReferenceEquals(s3,s4));

你会得到:

 True:False 
True:False
True:True


I have in the project the next line.

rdr = scomm.ExecuteReader();
log.Debug("log 1" + rdr.Read() + " " + rdr.GetString(0) + " " + String.ReferenceEquals(rdr.GetString(0), "SUCCES"));

and this is return in the logfile log 1True SUCCES False.

I do not understand why "String.ReferenceEquals(rdr.GetString(0), "SUCCES")" is not true.

Thank you in advance!

What I have tried:

I tried to verify the result of the sql command execution.

解决方案

See Object.ReferenceEquals Method (Object, Object) (System)[^].


Have also a look at this CodeProject article: Comparing Values for Equality in .NET: Identity and Equivalence[^].


Don't do things like that!
The problem is that you don't control the execution order: the compiler (and worse the optimiser) is at liberty to evaluate the "elements" of that string in any order it chooses - and it doesn't know that the Read method needs to be called first.
Try explicitly ordering the operations:

rdr = scomm.ExecuteReader();
bool isData = rdr.Read();
string s1 = rdr.GetString(0);
string s2 = String.ReferenceEquals(rdr.GetString(0), "SUCCES");
log.Debug("log 1" + isData + " " + s1 + " " + s2);


As to why they aren't the same reference, the intern pool for strings is only applied to string literals - not to strings that aren't defined at compile time. Try this:

        private string GC(string s1, string s2)
            {
            return s1 + s2;
            }
...
            string s1 = GC("hello", " there");
            string s2 = GC("hello", " there");
            string s3 = "hello there";
            string s4 = "hello there";

            Console.WriteLine("{0}:{1}", s1 == s2, string.ReferenceEquals(s1, s2));
            Console.WriteLine("{0}:{1}", s1 == s3, string.ReferenceEquals(s1, s3));
            Console.WriteLine("{0}:{1}", s3 == s4, string.ReferenceEquals(s3, s4));

And you will get:

True:False
True:False
True:True


这篇关于使用getstring方法的问题(c#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆