是一个字符串实际上是C#中的字符数组? [英] is a string actually an array of characters in C#?

查看:52
本文介绍了是一个字符串实际上是C#中的字符数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string str = "abcd"; 
for(int i=0;i<str.length;++i)>
console.write(str[i]); 



输出: abcd



看起来str实际上是一个数组字符!

这令人困惑!



我们没有声明任何数组,但为什么我们有str [0] = a等等......?!



[Agent_Spock]

- 添加代码括号


output: abcd

it seems like str is actually an array of characters!
this is being confusing!

we didn't declare any arrays, but how come we have str[0]=a and so on...?!

[Agent_Spock]
- Added code brackets

推荐答案

如果你只对C#(更具体地说是.NET框架)查看String的方式感兴趣 - 它不是数组。

字符串[index]有效,因为String类有一个实现它的索引器。 ..

If you only interested in the way how C# (and more specifically the .NET framework) looks at String - it is NOT an array.
The string[index] works because String class has an indexer to implement it...
public extern char this[int index]
{
}


这两个都是在同一时间。数组只不过是指向从基地址偏移的内存块的指针。



字符串是一个连续的内存块,从相同的地址开始,包含的是考虑过人物。对于每个字符,这些可以是一个或多个字节长。指针大小与字节大小相匹配。
It's both at the same time. An array is nothing but a pointer to a block of memory offset from a base address.

A string is a contiguous block of memory starting at the same address containing what is considered characters. These can be either one or more bytes long for each character. The pointer size matches the size of the characters in bytes.


数组只是类似数据类型的集合。字符串是一个内存块,它以有争议的方式包含字符。简单来说,字符串也称为char数组。



所以在上面的例子中你得到了那个输出。
Array is nothing but the collection of similar type of data . And a string is a block of memory which contains chars in contentious manner. In simple words a string is also known as a char array .

So in the above case you have got that output .


这篇关于是一个字符串实际上是C#中的字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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