Cannot use Classes on this ref ClassName (byRef) method Feature C# 7.2
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
|
show 5 more comments
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
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 needref
– Neil
Nov 23 '18 at 13:02
Why do you think you needrefhere? 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 refis available only for Value types.
– SeM
Nov 23 '18 at 13:06
|
show 5 more comments
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
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
c# c#-7.2
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 needref
– Neil
Nov 23 '18 at 13:02
Why do you think you needrefhere? 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 refis available only for Value types.
– SeM
Nov 23 '18 at 13:06
|
show 5 more comments
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 needref
– Neil
Nov 23 '18 at 13:02
Why do you think you needrefhere? 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 refis 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
|
show 5 more comments
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
refhere? 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 refis available only for Value types.– SeM
Nov 23 '18 at 13:06