Cannot use Classes on this ref ClassName (byRef) method Feature C# 7.2

105 views Asked by At

i was testin the new and shiny C# 7.1/7.2/7.3 features and when i was trying out the this ref Class, it was not working, meanwhile this ref int worked, do you guys have any idea on how to make it work with classes?

Example of the code:

    public static bool Works(this ref int i)
    {
        return i == 0;
    }

    public static bool DontWorks(this ref Test i)
    {
        return i.A == 0;
    }

    public class Test
    {
        public int A { get; set; }
    }

I am sorry that the title was a little bit bad, but i didnt know on how to make it better, feel free to give me suggestions or edit if you like.

Thank you for your time, and have a nice day.

1

There are 1 answers

1
jan.h On BEST ANSWER

Ref extension methods are only allowed on types known to be structs. This is intentional. The reasons behind this can be found in the feature proposal document.

Besides, regular class types are reference types. Passing a reference type as a parameter does not copy the object, unlike with value type parameters.