[转载]String.Empty、string=”” 和null的区别
String.Empty是string类的一个静态常量;
String.Empty和string=””区别不大,因为String.Empty的内部实现是:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public static readonly string Empty;//这就是String.Empty 那是只读的String类的成员,也是string的变量的默认值是什么呢?//String的构造函数static String(){ Empty = "";//Empty就是他"" WhitespaceChars = new char[] { '\t', '\n', '\v', '\f', '\r', ' ', '\x0085', '\x00a0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '', '\u2028', '\u2029', ' ', '' };} |
再看一段代码:
|
1
2
3
4
5
6
7
8
|
string s1 = "";string s2 = string.Empty;if (s1 == s2)<br>{ Console.WriteLine("一模一样!");} // 结果都是TrueConsole.WriteLine("".Equals(string.Empty));Console.WriteLine(object.ReferenceEquals(string.Empty, "")); |
既然String.Empty和string=””一样,同样需要占用内存空间,为什么推荐优先使用String.Empty ?
string.Empty只是让代码好读,防止代码产生歧义,比如说:
string s = ""; string s = " "; 这个不细心看,很难看出是空字符串还是空格字符。
如果判断一个字符串是否是空串,使用
if(s==String.Empty)和if(s==””)的效率是一样的,但是最高效的写法是if(s.Length==0)
string.IsNullOrEmpty的内部实现方式:
public static bool IsNullOrEmpty(string value)<br>{<br> if (value != null) { return (value.Length == 0); } return true;} |
而string str=null则是表示str未指向任何对象。
转自http://www.cnblogs.com/fanyong/archive/2012/11/01/2750163.html
[转载]String.Empty、string=”” 和null的区别的更多相关文章
- C# 中 string.Empty、""、null的区别
原文C# 中 string.Empty."".null的区别 一.string.Empty 和 "" 1.Empty是string类中的一个静态的只读字段,它是 ...
- C#中String.Empty,“”,NULL的区别
一.String.Empty String类的静态只读字段.定义如下: public static readonly string Empty; 二.“” 被赋值为“”的字符串变量,会在栈上保存一个地 ...
- C#中string.Empty和""、null的区别
string.Empty是string类的一个静态常量,而""则表示一个空字符串. string是一种特殊的引用类型,它的null值则表示没有分配内存. 使用ILSpy反编译Str ...
- C# 中 string.Empty、""、null的差别
一.string.Empty 和 "" 原文1 原文2 1. ...
- 重学c#系列——string.empty 和 "" 还有null[二十]
前言 简单整理一下string.empty 和 "" 还有 null的区别. 正文 首先null 和 string.empty 还有 "" 是不一样的. nul ...
- C#中string.Empty、""和null 之间的区别
1.C#中string.Empty.""和null 之间的区别 (http://blog.csdn.net/henulwj/article/details/7830615)
- C# null,string.Empty,"",DBNull 的区别
[null] null 在C# 中是一个关键字,表示不引用任何对象的空引用的文字值. null 是引用类型变量的默认值. 普通值类型不能为 null. null 在 VS 中定位不出来具体是怎么定义的 ...
- 转:String.Empty、string=”” 和null的区别
原文地址:http://www.cnblogs.com/fanyong/archive/2012/11/01/2750163.html String.Empty是string类的一个静态常量: Str ...
- String.Empty、string=”” 和null的区别
String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: 1 2 3 4 5 6 7 8 9 10 1 ...
随机推荐
- 在function module 中向数据库插入数据
http://www.sapjx.com/abap-function-module.html 1: 应该在function module 中向数据库插入数据
- Java 基础 面向对象和抽象类
面向对象变量 局部变量和成员变量区别 区别一:定义的位置不同 定义在类中的变量是成员变量 定义在方法中或者{}语句里面的变量是局部变量 区别二:在内存中的位置不同 成员变量存储在对内存的对象中 局部变 ...
- Python的Numpy库简述
numpy 是 python 的科学计算库import numpy as np 1.使用numpy读取txt文件 # dtype = "str":指定数据格式 # delimite ...
- max_execution_time with sleep
Under Linux, sleeping time is ignored, but under Windows, it counts as execution time. Note The set_ ...
- jquery事件重复绑定
本文实例分析了jQuery防止重复绑定事件的解决方法.分享给大家供大家参考,具体如下: 一.问题: 今天发现jQuery一个对象的事件可以重复绑定多次,当事件触发的时候会引起代码多遍执行. 下面是一个 ...
- Amber TUTORIAL 4b: Using Antechamber to Create LEaP Input Files for Simulating Sustiva (efavirenz)-RT complex using the General Amber Force Field (GAFF)
sustiva.pdb PDB: 1FKO Create parameter and coordinate files for Sustiva 1. 加氢: $ reduce sustiva.pdb ...
- cocos2d-x C++ iOS工程集成第三方支付宝支付功能
一.在支付宝开放平台下载支付宝SDK(https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.WWgVz8&tr ...
- PHP判断ip地址是否合法
1.获取真正ip地址 function get_ip(){ //判断服务器是否允许$_SERVER if(isset($_SERVER)){ if(isset($_SERVER[HTTP_X_FORW ...
- teragen/terasort_简化版
1, 关闭Hadoop安全模式 进入hdfs用户 su – hdfs Cd /opt/cloudera/parcels/CDH-5.12.1-1.cdh5.12.1.p0.3/bin hdfs dfs ...
- ideal使用eclipse快捷键
1.修改使用Eclipse风格的快捷键目的是习惯了使用eclipse的快捷键,在使用IDEA时不想重头记一套新的快捷键.按照下面的顺序操作File --> settings --> key ...