C # General Functions

Finding some help in C # generics. I work in Unity3D and execute the BiLerp function since Unity3D does not have it.

public Vector3 BiLerp(Vector2 _uv, Vector3 _00, Vector3 _01, Vector3 _10, Vector3 _11)
{
    return _00 * (1 - _uv.x) * (1 - _uv.y) +
           _10 * _uv.x * (1 - _uv.y) +
           _01 * _uv.y * (1 - _uv.x) +
           _11 * _uv.x * _uv.y;
}

However, I would like to make this feature a little more reliable.

  • I would like to make it general so that it accepts any type that can be multiplied by float
  • I am not sure if I should do it statically. I assume that since it does not use a member variable, then yes. If the class in which it is stored is also static, the class will be simple for math helpers omitted from unity (Matrix2x2), etc.
  • How does C # const work in this case?
  • In C ++, I passed parameters by reference. Will I fix it by assuming that C # is already doing this? and that the ref keyword is another dark magic tool?

, , . # 6 .

+5
4
  • , , , . , .

  • , , . (!) , , .

  • # consts ++, , (, , ). ​​

  • , Vector2 Vector3 , . , ref:

    public Vector3 BiLerp(
        ref Vector2 _uv, ref Vector3 _00, ref Vector3 _01,
        ref Vector3 _10, ref Vector3 _11)`
    

( ):

  • , .

  • , (.. ++).

, (, , ).

+6

, , float

, # . , #, CLR . , , , , .

, . , , -, .

, .

, , , .

.

# const ?

# const ++ const; . # const local - , . (, , , .) " , - , ", #.

, , ++ - const .

"readonly" , .

++ . , # ?

. .

ref - ?

ref - ++: . # , , . , # ++ " const", # , , , "const " ", , ".

+4

1) , , float

, #. T where T : float. , only class or interface could be specified as constraint.

2) , .... , ,

, 1 . , . , , ( ). , . - :

public static class Vector2Ext
{
    public static Vector3 BiLerp(
        this Vector2 _uv,
        Vector3 _00,
        Vector3 _01,
        Vector3 _10,
        Vector3 _11)
    {
        // your implementation
    }
}

BiLerp Vector2.

Vector2 vector2 = ...;
vector2.BiLerp(_00, _01, _10, _11);

, , , .

3) # const ?

, , . MSDN?

4) ++ . , , # ? ref - ?

. ref, . , VectorXD . , valuetype . , , , Vector3D . ref . , , . , MSDN ref.

+2

# , ++. generics, , ++. , , float, , float - ValueType. # ( .NET ) .

float double. # float double (, decimal).

+1

All Articles