zoukankan      html  css  js  c++  java
  • string.Empty与""的区别

    string.Empty不分配存储空间
          ""分配一个长度为空的存储空间   
          所以一般用string.Empty

    为了以后跨平台,还是用string.empty

    在 C# 中,大多数情况下 "" 和 string.Empty 可以互换使用。比如:
    string s = "";
    string s2 = string.Empty;

    if (s == string.Empty) {
      
    //
    }

    if语句成立


    判定为空字符串的几种写法,按照性能从高到低的顺序是:

    s.Length == 0      优于 s == string.Empty      优于 s == ""


    您关于String.Empty和Null的问题是这样的,这两个都是表示空字符串,其中有一个重点是string str1= String.Empty和 string str2=null 的区别,这样定义后,str1是一个空字符串,空字符串是一个特殊的字符串,只不过这个字符串的值为空,在内存中是有准确的指向的,string str2=null,这样定义后,只是定义了一个string 类的引用,str2并没有指向任何地方,在使用前如果不实例化的话,都将报错。textBox1.Text的值为零长度字符串 ""。

    String.Empty vs. ""

    So, I just converted a bunch of code that used "" as an empty string like this:

    if(myString == "") anotherString = "";

    to

    if(myString.Equals(String.Empty))   anotherString = String.Empty;

    and I'm wondering if I did the right thing, using String.Empty. I hate having quoted strings in code and prefer to stay away from them whenever possible.   This generally leads to code that is more strongly typed, and easier to maintain, so using String.Empty intuitively feels better than ““.   But, I've actually found a concrete reason to use String.Empty - I did some research and found that in a test, str.Equals(String.Empty) is faster than comparing to ""!    Well, okay. Research isn't the right word, “Doing one google search and accepting on faith the first post I saw” is slightly more accurate.   I even created a little graph of the claims in this post here, that String.Empty is faster.   I'm too lazy to do the test myself, so if you want to verify this, knock yourself out.   I do love making graphs though.

  • 相关阅读:
    存储过程简介
    微信公众号开发之授权获取用户信息
    微信公众号开发之自定义菜单
    微信公众号开发之如何实现消息交互
    VS2012创建ATL工程及使用MFC测试COM组件
    Regsvr32 在64位机器上的用法(转载)
    模块加载失败 请确保该二进制文件在指定的路径中 找不到指定的模块(转载)
    [内核驱动] miniFilter 内核层与应用程序通信
    [内核驱动] VS2012+WDK 8.0 Minifilter实现指定扩展名文件拒绝访问
    InstallShield 2015 生成单个EXE包和 MSI包
  • 原文地址:https://www.cnblogs.com/jiangchongwei/p/1273294.html
Copyright © 2011-2022 走看看