c# - 在 C# 公共(public)方法中,除了整数类型之外, `int` 表示什么?

标签 c# methods int

在 C# 中,当定义一个公共(public)方法时:

public int myMethod(String someString)
{  
   //code  
}

除了整数类型之外,int 还表示什么?让我感到困惑的是,在这种情况下,该方法使用字符串作为参数。

最佳答案

它是方法的返回类型。在本例中,一个 32 位有符号整数,范围为

-2,147,483,648 .. +2,147,483,647

它对应于 .NET 类型 System.Int32int 只是它的一个方便的 C# 别名。

你会返回这样的值

public int Square(int i)
{
    return i * i;
}

你可以这样调用它

int sqr = Square(7); // Returns 49
// Or
double d = Math.Sin(Square(3));

如果不需要返回值,可以安全地忽略它。

int i;
Int32.TryParse("123", out i); // We ignore the `bool` return value here.

如果您没有返回值,您可以使用关键字 void 代替类型。 void 不是真正的类型。

public void PrintSquare(int i)
{
    Console.WriteLine(i * i);
}

你会这样调用它

PrintSquare(7);

您示例中的方法接受一个 string 作为输入参数,并返回一个 int 作为结果。一个实际的例子是计算 string 中元音数量的方法。

public int NumberOfVowels(string s)
{
    const string vowels = "aeiouAEIOU";

    int n = 0;
    for (int i = 0; i < s.Length; i++) {
        if (vowels.Contains(s[i])) {
            n++;
        }
    }
    return n;
}

关于c# - 在 C# 公共(public)方法中,除了整数类型之外, `int` 表示什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9812992/

相关文章:

c# - 更改文件扩展名 SPListItem SharePoint 2007

ios - 如何正确弃用 Xcode 4 中的方法

Javascript 方法样式 : What is the difference between these two method styles?

arrays - 最大 i-j ,使得 A[i]>=A[j]

c# - 使用空元素反序列化 Xml

c# - 命名类型不用于构造函数注入(inject)

c# - 如何从 C# 中的 pdf 中提取图像?

java - 为什么我这里不能有 'void' 类型?

python - 如何将 boolean 值列表转换为单个 int,其中每个 boolean 值都被解释为位?

Python Pandas 数据帧 : unorderable types: str() > int()