ref readonly struct field VS regular readonly struct property? [on hold]Enum vs int wrapper structUsing...

How do ultrasonic sensors differentiate between transmitted and received signals?

Adding empty element to declared container without declaring type of element

node command while defining a coordinate in TikZ

Simple image editor tool to draw a simple box/rectangle in an existing image

Why are on-board computers allowed to change controls without notifying the pilots?

Meta programming: Declare a new struct on the fly

Did US corporations pay demonstrators in the German demonstrations against article 13?

How to check participants in at events?

What to do when my ideas aren't chosen, when I strongly disagree with the chosen solution?

A known event to a history junkie

Perfect riffle shuffles

How can I successfully establish a nationwide combat training program for a large country?

Have I saved too much for retirement so far?

In Star Trek IV, why did the Bounty go back to a time when whales were already rare?

Partial sums of primes

Bob has never been a M before

Is a naturally all "male" species possible?

Stereotypical names

Invariance of results when scaling explanatory variables in logistic regression, is there a proof?

What is the opposite of 'gravitas'?

Lifted its hind leg on or lifted its hind leg towards?

I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?

What does the "3am" section means in manpages?

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?



ref readonly struct field VS regular readonly struct property? [on hold]


Enum vs int wrapper structUsing readonly field instead of private setters in structDefining application flow in ASP.NET MVC5Writing to a newly created file, with support for unit testingStitching Linq with if blocksField Can Be Made ReadonlyRectangle ClassSingleton with readonly parametersShorthand private field caching of public propertyUnit testing a generic interface: proposal to avoid test duplication for different generic types













0












$begingroup$


I am implementing the Unit type (like in F# and other functional languages) in C#, I drafted the code below:



public readonly struct Unit : IEquatable<Unit>
{
public static readonly Unit Default = new Unit();

public override int GetHashCode() =>
0;

public override bool Equals(object obj) =>
obj is Unit;

public bool Equals(Unit other) =>
true;

public static bool operator ==(Unit left, Unit right) =>
true;

public static bool operator !=(Unit left, Unit right) =>
false;
}


However, I found another implementation which is pretty similar to mine:




public readonly struct Unit : IEquatable<Unit>
{
private static Unit _default = default;
public static ref readonly Unit Default => ref _default;

public bool Equals(Unit other) => true;
public override bool Equals(object obj) => obj is Unit;
public override int GetHashCode() => 0;
public static bool operator ==(in Unit first, in Unit second) => true;
public static bool operator !=(in Unit first, in Unit second) => false;
}



As you can see the main difference lies in the Default property implementation, I am wondering if there is any benefit on the second implementation which leverage the ref keyword.



If there is a lot of Unit.Default use in a codebase is it gonna save up a lot of memory / GC ticks?



Or is it just a micro optimization which is not worth it since a struct is gonna be de-allocated anyway at the end of the method which actually uses it?










share|improve this question











$endgroup$



put on hold as off-topic by t3chb0t, yuri, VisualMelon, Jesse C. Slicer, 200_success yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – VisualMelon, Jesse C. Slicer

If this question can be reworded to fit the rules in the help center, please edit the question.
















  • $begingroup$
    We also don't review code that isn't written by you. Please see the help center for more info on that. As such we can't really do a comparative review because one of the implementations isn't yours.
    $endgroup$
    – bruglesco
    yesterday
















0












$begingroup$


I am implementing the Unit type (like in F# and other functional languages) in C#, I drafted the code below:



public readonly struct Unit : IEquatable<Unit>
{
public static readonly Unit Default = new Unit();

public override int GetHashCode() =>
0;

public override bool Equals(object obj) =>
obj is Unit;

public bool Equals(Unit other) =>
true;

public static bool operator ==(Unit left, Unit right) =>
true;

public static bool operator !=(Unit left, Unit right) =>
false;
}


However, I found another implementation which is pretty similar to mine:




public readonly struct Unit : IEquatable<Unit>
{
private static Unit _default = default;
public static ref readonly Unit Default => ref _default;

public bool Equals(Unit other) => true;
public override bool Equals(object obj) => obj is Unit;
public override int GetHashCode() => 0;
public static bool operator ==(in Unit first, in Unit second) => true;
public static bool operator !=(in Unit first, in Unit second) => false;
}



As you can see the main difference lies in the Default property implementation, I am wondering if there is any benefit on the second implementation which leverage the ref keyword.



If there is a lot of Unit.Default use in a codebase is it gonna save up a lot of memory / GC ticks?



Or is it just a micro optimization which is not worth it since a struct is gonna be de-allocated anyway at the end of the method which actually uses it?










share|improve this question











$endgroup$



put on hold as off-topic by t3chb0t, yuri, VisualMelon, Jesse C. Slicer, 200_success yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – VisualMelon, Jesse C. Slicer

If this question can be reworded to fit the rules in the help center, please edit the question.
















  • $begingroup$
    We also don't review code that isn't written by you. Please see the help center for more info on that. As such we can't really do a comparative review because one of the implementations isn't yours.
    $endgroup$
    – bruglesco
    yesterday














0












0








0





$begingroup$


I am implementing the Unit type (like in F# and other functional languages) in C#, I drafted the code below:



public readonly struct Unit : IEquatable<Unit>
{
public static readonly Unit Default = new Unit();

public override int GetHashCode() =>
0;

public override bool Equals(object obj) =>
obj is Unit;

public bool Equals(Unit other) =>
true;

public static bool operator ==(Unit left, Unit right) =>
true;

public static bool operator !=(Unit left, Unit right) =>
false;
}


However, I found another implementation which is pretty similar to mine:




public readonly struct Unit : IEquatable<Unit>
{
private static Unit _default = default;
public static ref readonly Unit Default => ref _default;

public bool Equals(Unit other) => true;
public override bool Equals(object obj) => obj is Unit;
public override int GetHashCode() => 0;
public static bool operator ==(in Unit first, in Unit second) => true;
public static bool operator !=(in Unit first, in Unit second) => false;
}



As you can see the main difference lies in the Default property implementation, I am wondering if there is any benefit on the second implementation which leverage the ref keyword.



If there is a lot of Unit.Default use in a codebase is it gonna save up a lot of memory / GC ticks?



Or is it just a micro optimization which is not worth it since a struct is gonna be de-allocated anyway at the end of the method which actually uses it?










share|improve this question











$endgroup$




I am implementing the Unit type (like in F# and other functional languages) in C#, I drafted the code below:



public readonly struct Unit : IEquatable<Unit>
{
public static readonly Unit Default = new Unit();

public override int GetHashCode() =>
0;

public override bool Equals(object obj) =>
obj is Unit;

public bool Equals(Unit other) =>
true;

public static bool operator ==(Unit left, Unit right) =>
true;

public static bool operator !=(Unit left, Unit right) =>
false;
}


However, I found another implementation which is pretty similar to mine:




public readonly struct Unit : IEquatable<Unit>
{
private static Unit _default = default;
public static ref readonly Unit Default => ref _default;

public bool Equals(Unit other) => true;
public override bool Equals(object obj) => obj is Unit;
public override int GetHashCode() => 0;
public static bool operator ==(in Unit first, in Unit second) => true;
public static bool operator !=(in Unit first, in Unit second) => false;
}



As you can see the main difference lies in the Default property implementation, I am wondering if there is any benefit on the second implementation which leverage the ref keyword.



If there is a lot of Unit.Default use in a codebase is it gonna save up a lot of memory / GC ticks?



Or is it just a micro optimization which is not worth it since a struct is gonna be de-allocated anyway at the end of the method which actually uses it?







c# .net functional-programming reference .net-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









bruglesco

1,6592824




1,6592824










asked yesterday









Ehouarn PerretEhouarn Perret

279111




279111




put on hold as off-topic by t3chb0t, yuri, VisualMelon, Jesse C. Slicer, 200_success yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – VisualMelon, Jesse C. Slicer

If this question can be reworded to fit the rules in the help center, please edit the question.







put on hold as off-topic by t3chb0t, yuri, VisualMelon, Jesse C. Slicer, 200_success yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – VisualMelon, Jesse C. Slicer

If this question can be reworded to fit the rules in the help center, please edit the question.












  • $begingroup$
    We also don't review code that isn't written by you. Please see the help center for more info on that. As such we can't really do a comparative review because one of the implementations isn't yours.
    $endgroup$
    – bruglesco
    yesterday


















  • $begingroup$
    We also don't review code that isn't written by you. Please see the help center for more info on that. As such we can't really do a comparative review because one of the implementations isn't yours.
    $endgroup$
    – bruglesco
    yesterday
















$begingroup$
We also don't review code that isn't written by you. Please see the help center for more info on that. As such we can't really do a comparative review because one of the implementations isn't yours.
$endgroup$
– bruglesco
yesterday




$begingroup$
We also don't review code that isn't written by you. Please see the help center for more info on that. As such we can't really do a comparative review because one of the implementations isn't yours.
$endgroup$
– bruglesco
yesterday










0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Webac Holding Inhaltsverzeichnis Geschichte | Organisationsstruktur | Tochterfirmen |...

What's the meaning of a knight fighting a snail in medieval book illustrations?What is the meaning of a glove...

Salamanca Inhaltsverzeichnis Lage und Klima | Bevölkerungsentwicklung | Geschichte | Kultur und...