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);

1st Jan 2023, 10:01 PM
Giovanni Paolo Balestriere
Giovanni Paolo Balestriere - avatar
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.
2nd Jan 2023, 2:14 AM
Vitor
Vitor - avatar
+ 2
I guess I'd say "because that's the way C# works". The syntax is the syntax.
1st Jan 2023, 10:54 PM
Ausgrindtube
Ausgrindtube - avatar