惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 焰凌

Poor code: Dynamic changing of table schema When we need to inherit from WSS WebPart class? The issue of upload big file on SharePoint with IIS7 Why no effects when change the "Site Master Page Settings" from "Site Actions - Site Settings - Look and Feel - Master Page"? 怎样在MOSS2007和WSS3.0中修改服务帐户和密码 HttpApplication机制 "NOT IN", "JOIN...IS NULL", "NOT EXISTS" 之间的效率对比 为SharePoint新项目做准备 MOSS 2007 SSP (1) How to create and delete a SharePoint site by web application Search user of specific domain name Received DGT's document 水晶报表PUSH模式多个表数据的显示 上海电信之具有地域歧视的霸王条款 来上海一年多了,第一次感冒! 搬家了,没宽带了 工作以后 找不到状态了! 页面缓存的困扰
differentia of [string str=null;] and [string str="";]
焰凌 · 2006-07-25 · via 博客园 - 焰凌

今天在群里有个人问了这样一个问题,
“string str=null;和string str="";有什么不同,内存的分配有什么不同?”

乍一看,感觉有些知道,但是又比较模糊,特意查了一些资料。才知道了其中的不同。
首先,C#中的string虽然是表示字符串,但是其底层的实现仍然是字符数组。并且string是引用类型,其底层的实现必然是指针,因为string的底层实现,就是C中的传统字符串实现,指向字符数组的指针。
至此,string str=null;和string str="";的区别就可以比较清晰的看到了。
string str=null;的实质是 char *ps = 0;
string str="";  的实质是 char *ps=""; 或 char *ps="\0";
也就是说
string str=null;表示str是一个为0的指针。不分配任何内存空间。
string str="";  表示str指向首元素为0(字符串结束标志)的字符数组。会分配内存空间,但是该内存空间的首位为0。