Last active
January 27, 2020 22:57
-
-
Save hk1ll3r/a20c84a6101b2362fa5565a59d6a0794 to your computer and use it in GitHub Desktop.
gist to demonstrate c# 7.0's new value tuple syntax for returning multiple values from functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public struct Vector2 { | |
public float x; | |
public float y; | |
public Vector2(float x, float y) { | |
this.x = x; | |
this.y = y; | |
} | |
} | |
public class ReturnMultipleValuesTest { | |
static void Main() { | |
var t = new ReturnMultipleValuesTest(); | |
t._x = 5f; t._y = 9f; | |
Console.WriteLine(string.Format("GetPositionX: {0}", t.GetPositionX())); | |
Console.WriteLine(string.Format("GetPositionY: {0}", t.GetPositionY())); | |
float tmpX, tmpY; | |
t.GetPositionWithOutParams(out tmpX, out tmpY); | |
Console.WriteLine(string.Format("GetPosition: x:{0} y:{1}", tmpX, tmpY)); | |
Tuple<float, float> tuple = t.GetPositionRefTuple(); | |
Console.WriteLine(string.Format("GetPositionRefTuple: x:{0} y:{1}", tuple.Item1, tuple.Item2)); | |
(float tX, float tY) = t.GetPositionValueTuple(); | |
Console.WriteLine(string.Format("GetPositionValueTuple: x:{0} y:{1}", tX, tY)); | |
ValueTuple<float, float> vt = t.GetPositionValueTuple(); | |
Console.WriteLine(string.Format("GetPositionValueTupleAlternateSyntax: x:{0} y:{1}", vt.Item1, vt.Item2)); | |
} | |
float _x, _y; | |
public float GetPositionX() => _x; | |
public float GetPositionY() => _y; | |
public void GetPositionWithOutParams(out float x, out float y) { | |
x = _x; | |
y = _y; | |
} | |
public Vector2 GetPositionVector() { | |
return new Vector2(_x, _y); | |
} | |
public Tuple<float, float> GetPositionRefTuple() { | |
return Tuple.Create(_x, _y); | |
} | |
public ValueTuple<float, float> GetPositionValueTuple() { | |
return ValueTuple.Create(_x, _y); | |
} | |
// Is exactly the same as GetPositionVT | |
public (float, float) GetPositionValueTupleAlternateSyntax() { | |
return (_x, _y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment