<i id='rtUBe'><tr id='rtUBe'><dt id='rtUBe'><q id='rtUBe'><span id='rtUBe'><b id='rtUBe'><form id='rtUBe'><ins id='rtUBe'></ins><ul id='rtUBe'></ul><sub id='rtUBe'></sub></form><legend id='rtUBe'></legend><bdo id='rtUBe'><pre id='rtUBe'><center id='rtUBe'></center></pre></bdo></b><th id='rtUBe'></th></span></q></dt></tr></i><div id='rtUBe'><tfoot id='rtUBe'></tfoot><dl id='rtUBe'><fieldset id='rtUBe'></fieldset></dl></div>

<tfoot id='rtUBe'></tfoot>

<small id='rtUBe'></small><noframes id='rtUBe'>

    • <bdo id='rtUBe'></bdo><ul id='rtUBe'></ul>
    1. <legend id='rtUBe'><style id='rtUBe'><dir id='rtUBe'><q id='rtUBe'></q></dir></style></legend>

        C#将char和char数组分配给字符串?

        C# assign char and char array to string?(C#将char和char数组分配给字符串?)

            <bdo id='Pe7Xg'></bdo><ul id='Pe7Xg'></ul>
              1. <tfoot id='Pe7Xg'></tfoot>
                  <tbody id='Pe7Xg'></tbody>

                  <legend id='Pe7Xg'><style id='Pe7Xg'><dir id='Pe7Xg'><q id='Pe7Xg'></q></dir></style></legend>
                  <i id='Pe7Xg'><tr id='Pe7Xg'><dt id='Pe7Xg'><q id='Pe7Xg'><span id='Pe7Xg'><b id='Pe7Xg'><form id='Pe7Xg'><ins id='Pe7Xg'></ins><ul id='Pe7Xg'></ul><sub id='Pe7Xg'></sub></form><legend id='Pe7Xg'></legend><bdo id='Pe7Xg'><pre id='Pe7Xg'><center id='Pe7Xg'></center></pre></bdo></b><th id='Pe7Xg'></th></span></q></dt></tr></i><div id='Pe7Xg'><tfoot id='Pe7Xg'></tfoot><dl id='Pe7Xg'><fieldset id='Pe7Xg'></fieldset></dl></div>

                  <small id='Pe7Xg'></small><noframes id='Pe7Xg'>

                  本文介绍了C#将char和char数组分配给字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..
                          char character = 'c';
                          string str = null;
                          str = character.ToString();//this is ok
                  
                          char[] arrayChar = { 'a', 'b', 'c', 'd' };
                          string str2 = null;
                          str2 = string.Copy(arrayChar.ToString());//this is not ok
                          str2 = arrayChar.ToString();//this is not ok.
                  

                  我正在尝试将 char 数组转换为字符串,但最后两次尝试不起作用.我找到的其他来源,他们必须创建新的字符串类型,但我不知道为什么.谁能给我一点解释,谢谢.

                  I'm trying to converting char array to string, but the last two attempts don't work. Other source I found and they have to create new string type, but I don't know why. Can someone give me little explaination, thanks.

                  推荐答案

                  你需要构造一个新的字符串.

                  You need to construct a new string.

                  执行 arrayChar.ToString() 调用 char[] 类型的ToString"方法,该方法不会重载以从字符构造字符串,而是构造一个指定类型为字符数组的字符串.这不会给你你想要的行为.

                  Doing arrayChar.ToString() calls the "ToString" method for the char[] type, which is not overloaded to construct a string out the characters, but rather to construct a string that specifies that the type is an array of characters. This will not give you the behavior that you desire.

                  然而,通过 str2 = new string(arrayChar); 构造一个新字符串,会得到你想要的行为.

                  Constructing a new string, via str2 = new string(arrayChar);, however, will give you the behavior you desire.

                  问题在于,在 C# 中(与 C++ 不同),字符串与字符数组不同.这是两种截然不同的类型(即使它们可以表示相同的数据).字符串可以枚举为字符(String 实现 IEnumerable<Char>),但就 CLR 而言,它与字符的类型不同.进行转换需要代码在两者之间进行转换——字符串构造函数提供了这种机制.

                  The issue is that, in C# (unlike C++), a string is not the same as an array of characters. These are two distinctly different types (even though they can represent that same data). Strings can be enumerated as characters (String implements IEnumerable<Char>), but is not, as far as the CLR is concerned, the same type as characters. Doing a conversion requires code to convert between the two - and the string constructor provides this mechanism.

                  这篇关于C#将char和char数组分配给字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)

                    <tbody id='TvOIk'></tbody>
                    <legend id='TvOIk'><style id='TvOIk'><dir id='TvOIk'><q id='TvOIk'></q></dir></style></legend>

                      <i id='TvOIk'><tr id='TvOIk'><dt id='TvOIk'><q id='TvOIk'><span id='TvOIk'><b id='TvOIk'><form id='TvOIk'><ins id='TvOIk'></ins><ul id='TvOIk'></ul><sub id='TvOIk'></sub></form><legend id='TvOIk'></legend><bdo id='TvOIk'><pre id='TvOIk'><center id='TvOIk'></center></pre></bdo></b><th id='TvOIk'></th></span></q></dt></tr></i><div id='TvOIk'><tfoot id='TvOIk'></tfoot><dl id='TvOIk'><fieldset id='TvOIk'></fieldset></dl></div>
                      <tfoot id='TvOIk'></tfoot>
                        <bdo id='TvOIk'></bdo><ul id='TvOIk'></ul>
                        • <small id='TvOIk'></small><noframes id='TvOIk'>