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












-1















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.










share|improve this question




















  • 1





    Please paste the code into your question rather than using a screenshot. It helps people copy and paste your code to test it themselves.

    – Gabriel Luci
    Nov 23 '18 at 12:59











  • Extension functions don't need ref

    – Neil
    Nov 23 '18 at 13:02











  • Why do you think you need ref here? Your code will work fine without it.

    – Patrick Hofman
    Nov 23 '18 at 13:03













  • @GabrielLuci Edited the question thank you.

    – axd 221
    Nov 23 '18 at 13:04






  • 1





    this ref is available only for Value types.

    – SeM
    Nov 23 '18 at 13:06
















-1















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.










share|improve this question




















  • 1





    Please paste the code into your question rather than using a screenshot. It helps people copy and paste your code to test it themselves.

    – Gabriel Luci
    Nov 23 '18 at 12:59











  • Extension functions don't need ref

    – Neil
    Nov 23 '18 at 13:02











  • Why do you think you need ref here? Your code will work fine without it.

    – Patrick Hofman
    Nov 23 '18 at 13:03













  • @GabrielLuci Edited the question thank you.

    – axd 221
    Nov 23 '18 at 13:04






  • 1





    this ref is available only for Value types.

    – SeM
    Nov 23 '18 at 13:06














-1












-1








-1








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.










share|improve this question
















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.







c# c#-7.2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 13:02







axd 221

















asked Nov 23 '18 at 12:58









axd 221axd 221

82




82








  • 1





    Please paste the code into your question rather than using a screenshot. It helps people copy and paste your code to test it themselves.

    – Gabriel Luci
    Nov 23 '18 at 12:59











  • Extension functions don't need ref

    – Neil
    Nov 23 '18 at 13:02











  • Why do you think you need ref here? Your code will work fine without it.

    – Patrick Hofman
    Nov 23 '18 at 13:03













  • @GabrielLuci Edited the question thank you.

    – axd 221
    Nov 23 '18 at 13:04






  • 1





    this ref is available only for Value types.

    – SeM
    Nov 23 '18 at 13:06














  • 1





    Please paste the code into your question rather than using a screenshot. It helps people copy and paste your code to test it themselves.

    – Gabriel Luci
    Nov 23 '18 at 12:59











  • Extension functions don't need ref

    – Neil
    Nov 23 '18 at 13:02











  • Why do you think you need ref here? Your code will work fine without it.

    – Patrick Hofman
    Nov 23 '18 at 13:03













  • @GabrielLuci Edited the question thank you.

    – axd 221
    Nov 23 '18 at 13:04






  • 1





    this ref is available only for Value types.

    – SeM
    Nov 23 '18 at 13:06








1




1





Please paste the code into your question rather than using a screenshot. It helps people copy and paste your code to test it themselves.

– Gabriel Luci
Nov 23 '18 at 12:59





Please paste the code into your question rather than using a screenshot. It helps people copy and paste your code to test it themselves.

– Gabriel Luci
Nov 23 '18 at 12:59













Extension functions don't need ref

– Neil
Nov 23 '18 at 13:02





Extension functions don't need ref

– Neil
Nov 23 '18 at 13:02













Why do you think you need ref here? Your code will work fine without it.

– Patrick Hofman
Nov 23 '18 at 13:03







Why do you think you need ref here? Your code will work fine without it.

– Patrick Hofman
Nov 23 '18 at 13:03















@GabrielLuci Edited the question thank you.

– axd 221
Nov 23 '18 at 13:04





@GabrielLuci Edited the question thank you.

– axd 221
Nov 23 '18 at 13:04




1




1





this ref is available only for Value types.

– SeM
Nov 23 '18 at 13:06





this ref is available only for Value types.

– SeM
Nov 23 '18 at 13:06












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer
























  • Thank you for explaining me and giving the material to read about, learned a lot reading the links you gave me.

    – axd 221
    Nov 23 '18 at 13:15












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447167%2fcannot-use-classes-on-this-ref-classname-byref-method-feature-c-sharp-7-2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.






share|improve this answer
























  • Thank you for explaining me and giving the material to read about, learned a lot reading the links you gave me.

    – axd 221
    Nov 23 '18 at 13:15
















1














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.






share|improve this answer
























  • Thank you for explaining me and giving the material to read about, learned a lot reading the links you gave me.

    – axd 221
    Nov 23 '18 at 13:15














1












1








1







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.






share|improve this 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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 13:07









jan.hjan.h

511511




511511













  • Thank you for explaining me and giving the material to read about, learned a lot reading the links you gave me.

    – axd 221
    Nov 23 '18 at 13:15



















  • Thank you for explaining me and giving the material to read about, learned a lot reading the links you gave me.

    – axd 221
    Nov 23 '18 at 13:15

















Thank you for explaining me and giving the material to read about, learned a lot reading the links you gave me.

– axd 221
Nov 23 '18 at 13:15





Thank you for explaining me and giving the material to read about, learned a lot reading the links you gave me.

– axd 221
Nov 23 '18 at 13:15




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447167%2fcannot-use-classes-on-this-ref-classname-byref-method-feature-c-sharp-7-2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings