C
C#2y ago
Dultus

❔ Is there a way to remove brackets from empty methods?

E.g.
public MismatchedEnumLengthsException() : base("Length of values and enums are different. You are required to pass as many values as there are enums")
{
}
public MismatchedEnumLengthsException() : base("Length of values and enums are different. You are required to pass as many values as there are enums")
{
}
I don't need those brackets. Can't I make this look prettier?
19 Replies
Angius
Angius2y ago
No
JochCool
JochCool2y ago
As far as I know, no
TheRanger
TheRanger2y ago
why would you do that?
Angius
Angius2y ago
Most you can do is
public MismatchedEnumLengthsException() : base("Length of values and enums are different. You are required to pass as many values as there are enums"){}
public MismatchedEnumLengthsException() : base("Length of values and enums are different. You are required to pass as many values as there are enums"){}
to put it all on the same line
Dultus
DultusOP2y ago
To save two lines for every 'empty' method that I don't need. Yeah, figured as much. Though CodeRush automatically "fixes" that and puts them in two new lines. x)
Thinker
Thinker2y ago
public Ctor(int x) : base(x); is a thing I wish you could do but you unfortunately can't rainowo
Angius
Angius2y ago
PRs welcome when
HtmlCompiler
HtmlCompiler2y ago
😈 public MismatchedEnumLengthsException() : base("lol") => _ = this; public MismatchedEnumLengthsException() : base("lol") => = new {}; public MismatchedEnumLengthsException() : base("lol") => GetType(); public MismatchedEnumLengthsException() : base("lol") => Array.Empty<int>(); public MismatchedEnumLengthsException() : base("lol") => ((Action?)default)?.Invoke(); i remember i did something like calling an empty action or something, somewhere
jcotton42
jcotton422y ago
Just put them together at the end of the same line
TheBoxyBear
TheBoxyBear2y ago
Is there actually a reason you can inline methods but not constructors?
jcotton42
jcotton422y ago
wdym?
TheBoxyBear
TheBoxyBear2y ago
public string Foo() => "foo"; Or in the case of a constructor would be public Foo() => Prop = 1;
jcotton42
jcotton422y ago
pretty sure you can do that?
TheBoxyBear
TheBoxyBear2y ago
Right, just remembered my bad
MODiX
MODiX2y ago
jcotton42#1663
REPL Result: Success
public class Foo {
public int I { get; set; }
public Foo(int i) => I = i;
}
public class Foo {
public int I { get; set; }
public Foo(int i) => I = i;
}
Compile: 426.565ms | Execution: 37.465ms | React with ❌ to remove this embed.
HtmlCompiler
HtmlCompiler2y ago
like, i wrote 5 inline constructors just above your post...................
Angius
Angius2y ago
You can do cool stuff with inline ctors and tuples
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
class Foo
{
public int A { get; set; }
public int B { get; set; }

public Foo(int a, int b) => (A, B) = (a, b);
}

new Foo(7, 8)
class Foo
{
public int A { get; set; }
public int B { get; set; }

public Foo(int a, int b) => (A, B) = (a, b);
}

new Foo(7, 8)
Result: Foo
{
"a": 7,
"b": 8
}
{
"a": 7,
"b": 8
}
Compile: 490.760ms | Execution: 52.068ms | React with ❌ to remove this embed.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?