简体   繁体   中英

How to return value from function? (translate actionscript to c#)

So... I want to return value when C# function is called. I need a code example (simple summ of a,b values will be ok) Please help

I need something like this ( I know ActionScript so I will write in it):

public function sum(valueA:int, valueB:int):int 
{
   var summ:int = valueA + valueB;
   return summ;
}

How to translate it into C#?

Here:

public int sum(int valueA, int valueB)
{
    int summ = valueA + valueB;
    return summ;
}

Differences to note:

  • The return type is declared immediately after the public visiblity qualifier
  • Variable types are declared before them

As a side-note, C# (3.0 above) also supports the var keyword when declaring variables, which means that you can write:

public int Sum(int valueA, int valueB) { 
  var summ = valueA + valueB; 
  return summ; 
} 

The meaning of var is porbably different than in ActionScript - it looks at the expression used to initialize the variable and uses the type of the expression (so the code is statically-typed). In the example above, the type of summ will be int just like in the version posted by Oded.

(This is often a confusing thing for people with background in dynamic languages, so I thought it would be useful to mention this, especially since var is also a keyword in ActionScript).

Or even so =)

public int Sum ( int valueA, int valueB ) { return valueA + valueB; }

if you don't need to store a result in a function for some purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM