0
C# Getting a raise
why can i do this: static void Sqr(ref int x) { x = x * x; } static void Main(string[] args) { int a = 3; Sqr(ref a); Console.WriteLine(a); but not this one: static void Sqr(ref int x) { x * x; } static void Main(string[] args) { int a = 3; Sqr(ref a); Console.WriteLine(a);
2 odpowiedzi
+ 3
In programming languages, words and symbols and the order they appear change how they are interpreted.
In your first example (x*x), you declared a pointer (using the *) x of type x (which is not what you wanted and doesn't even make sense syntactically speaking).
However,
x*=x
would have the same effect as x=x*x. I think that's what I wanted to do in the first example.
+ 2
I guess I'd say "because that's the way C# works". The syntax is the syntax.