在 VB.NET 字符数组中添加“c”有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19522703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:35:01  来源:igfitidea点击:

What does adding a "c" do in a VB.NET character array?

vb.netstring

提问by n00b

I would like to use the String method IndexOfAny to check if a character exists in a specified string.

我想使用 String 方法 IndexOfAny 来检查指定字符串中是否存在字符。

Examples I've found online, of using the IndexOfAny method include a "c" after each character in the character array when using VB.NET. However, when I look at examples of simple character arrays in VB.NET, I dont see any such "c" after each character. What does the "c" do? Is it optional?

我在网上找到的使用 IndexOfAny 方法的示例在使用 VB.NET 时在字符数组中的每个字符后面都包含一个“c”。但是,当我查看 VB.NET 中简单字符数组的示例时,我没有在每个字符后看到任何这样的“c”。“c”有什么作用?它是可选的吗?

Dim s1 As String = "Darth is not my father."
' Find the first index of either "x" or "n"
Dim i1 As Integer = s1.IndexOfAny(New Char() {"x"c, "n"c})

回答by vcsjones

That is a suffix for a literal of type System.Char. So

这是 System.Char 类型文字的后缀。所以

Dim foo As Char = "x"c

Will compile (when Option Strict is set to either On or Off). Without the c, it would be interpreted as a string. For more information about literal suffixes in VB.NET, take a look at the MSDN page, "Constant and Literal Data Types".

将编译(当 Option Strict 设置为 On 或 Off 时)。如果没有c,它将被解释为字符串。有关 VB.NET 中文字后缀的更多信息,请查看 MSDN 页面“常量和文字数据类型”。