Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a C# public method, what does the `int` indicate apart from the type integer?

Tags:

methods

c#

int

In C#, when defining a public method like:

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

What does the int indicate apart from the type integer? What confuses me is that the method is using a String as arguments in this case.

like image 777
Arthur Mamou-Mani Avatar asked Mar 21 '12 21:03

Arthur Mamou-Mani


People also ask

Why is there a slash in AC?

Senior Member. You read it as "ei-cee" (no "slash" pronounced). In terms of distinguishing between "air conditioning" and "air conditioner," I can think of an example like "Today, I bought a new air conditioner" ("conditioning" not allowed). I personally would not say "Today, I bought a new AC."

What does AC mean?

AC stands for 'alternating current' which means the current constantly changes direction. Mains electricity is an AC supply, and the UK mains supply is about 230 volts. It has a frequency of 50Hz (50 hertz), which means it changes direction and back again 50 times a second.

What does AC mean on a letter?

The label "in care of" simply means you are sending the letter or package to an addressee that is accepting the correspondence for the intended recipient. People often use the abbreviation "c/o" to send mail to someone they don't have an address for or to send mail to themselves at someone else's residence.

Why is it called AC?

Air conditioning, often abbreviated as A/C or AC, is the process of removing heat from an enclosed space to achieve a more comfortable interior environment (sometimes referred to as 'comfort cooling') and in some cases also strictly controlling the humidity of internal air.


1 Answers

It is the return type of the method. In this case a 32-bit signed integer with a range of

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

It corresponds to the .NET type System.Int32. int is just a handy C# alias for it.

You would return a value like this

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

And you could call it like this

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

If you do not need the return value, you can safely ignore it.

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

If you have no return value you would use the keyword void in place of the type. void is not a real type.

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

And you would call it like this

PrintSquare(7);

The method in your example accepts a string as input parameter and returns an int as result. A practical example would be a method that counts the number of vowels in a 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;
}
like image 177
Olivier Jacot-Descombes Avatar answered Sep 21 '22 15:09

Olivier Jacot-Descombes