c#ToChar from Array显示UTF或Ascii值

时间:2014-08-02 08:56:24

标签: c# arrays utf-8 ascii

我正在与数组进行比较,以查看数组中哪个值(char)更大。它工作正常,直到我认为我会添加它所比较的​​值的表示。

using System;

class ArrayEx3
{
    static void Main()
    {
        char[] array1 = { 'a', 'E', 'i', 'o', 'u' };
        char[] array2 = { 'A', 'e', 'I', 'O', 'U' };

        for (int i = 0; i < array1.Length; i++)
        {
            if (array1[i] > array2[i])
            {
                Console.WriteLine(" {0} is {2} bigger than {1} which is {3}", array1[i], array2[i],((int)array2[i].ToChar()),((int)array2[i].ToChar()));
            }
            else
            {
                Console.WriteLine(" {0} is  bigger than {1}", array2[i], array1[i]);
            }
        }
    }
}

我收到此错误。

C:\Users\Sayth\Documents\Scripts>csc ArrayEx3.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.33440
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

ArrayEx3.cs(14,104): error CS1061: 'char' does not contain a definition for
        'ToChar' and no extension method 'ToChar' accepting a first argument of
        type 'char' could be found (are you missing a using directive or an
        assembly reference?)
ArrayEx3.cs(14,130): error CS1061: 'char' does not contain a definition for
        'ToChar' and no extension method 'ToChar' accepting a first argument of
        type 'char' could be found (are you missing a using directive or an
        assembly reference?)

C:\Users\Sayth\Documents\Scripts>

检查转换ToChar文档虽然http://msdn.microsoft.com/en-us/library/4db6awt7(v=vs.110).aspx显示ToChar在系统空间中,但为什么我会这样做?

1 个答案:

答案 0 :(得分:2)

你需要这样称呼它:

Convert.ToChar(array2[i]);

这不是一种扩展方法。

顺便说一下,将char转换为char是没有意义的......如果你想获得你的char的ASCII值,只需将其转换为int。< / p>
Console.WriteLine(" {0} is {2} bigger than {1} which is {3}", array1[i], array2[i], (int)array1[i], (int)array2[i]);