0

I'm successfully overloading operator like this:

public static Vector3D operator +(Vector3D v1, Vector3D v2){ return new Vector3D(v1._x + v2._x, v1._y + v2._y, v1._z + v2._z); } 

I've read on this page that i could also do it like this:

public static Vector3D operator +(Vector3D v1, Vector3D v2) => new Vector3D(v1._x + v2._x, v1._y + v2._y, v1._z + v2._z); 

But in this case, under => I got error saying "; expected". What am I missing?

7
  • 3
    It probably because of the C# version difference or Visual Studio version Commented May 10, 2017 at 10:52
  • As @Ian has mentioned, you're probably running a version of Visual Studio that's using an older version of C#. Commented May 10, 2017 at 10:53
  • thanks, what version of C# then it needs? Commented May 10, 2017 at 10:55
  • @PawełAudionysos what VS version do you use? The latest C# should do... but it really depends on whether you have it or not Commented May 10, 2017 at 10:58
  • 1
    You can probably hack your .csproj file to use a newer msbuild/sdk, but you won't get much help from intellisense in Visual Studio so any syntax completion is going to fight you. I would suggest you stay away from the new syntax until you update to a newer visual studio. Commented May 10, 2017 at 11:06

1 Answer 1

1

Looking at the link you have posted the example:

public static Complex operator +(Complex c1, Complex c2) => new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary); // Override ToString() to display a complex number // in the traditional format: public override string ToString() => $"{this.real} + {this.imaginary}"; 

strongly suggest that this feature can be applied from the Version of C# 6.0 since also the use of $ as substitute for String.Format exist from the Version of C# 6.0 on and higher.

Try using Visual Studio 2015 to make it work. I did, it worked

Is it possible to update C# version for VS 2012?

Yes, apparently it is. How to do it you will find in this post

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.