C
C#11mo ago
novial

better way to do this?

No description
19 Replies
Thinker
Thinker11mo ago
Just this constructor? In C# 12 you can do
public class BoxCollider2D(Vector2 size)
{
private Vector2 size = size;
}
public class BoxCollider2D(Vector2 size)
{
private Vector2 size = size;
}
Otherwise, there's not really any better way to write such a small constructor
mtreit
mtreit11mo ago
Shouldn't that just be:
public class BoxCollider2D(Vector2 size)
{
}
public class BoxCollider2D(Vector2 size)
{
}
Thinker
Thinker11mo ago
Yeah it could
jcotton42
jcotton4211mo ago
there's a slight difference, yours doesn't necessarily create a field but for most cases they're interchangeable
mtreit
mtreit11mo ago
It's unclear from the OP what this.size is intended to be...with the way people case things around here it could be a property 😄
nohopestage
nohopestage11mo ago
I like how in Kotlin you can do this:
class BoxCollider2D(private val size: Vector2) { }
class BoxCollider2D(private val size: Vector2) { }
Wish C# had that feature So size would be a private field?
Angius
Angius11mo ago
No modifiers, alas, but that's what primary ctors are
class BoxCollider2D(Vector2 size) {}
class BoxCollider2D(Vector2 size) {}
nohopestage
nohopestage11mo ago
Yeah, still pretty nice So they're not object members
MODiX
MODiX11mo ago
Angius
sharplab.io (click here)
public class Foo(int bar) {
public void Write() => Console.Write(bar);
};
public class Foo(int bar) {
public void Write() => Console.Write(bar);
};
Try the /sharplab command! | React with ❌ to remove this embed.
Angius
Angius11mo ago
The compiler will generate
public class Foo
{
private int <bar>P;

public Foo(int bar)
{
<bar>P = bar;
base..ctor();
}

public void Write()
{
Console.Write(<bar>P);
}
}
public class Foo
{
private int <bar>P;

public Foo(int bar)
{
<bar>P = bar;
base..ctor();
}

public void Write()
{
Console.Write(<bar>P);
}
}
nohopestage
nohopestage11mo ago
Oh, I see
The compiler creates storage for any primary constructors only when that parameter is accessed in the body of a member of your type. Otherwise, the primary constructor parameters aren't stored in the object. Primary constructor parameters aren't members of the class. For example, a primary constructor parameter named param can't be accessed as this.param. Weird So they're not, but they also are Or the other way around
Angius
Angius11mo ago
Yeah, if you just do
public class Foo(int bar);
public class Foo(int bar);
the compiler will generate
public class Foo
{
public Foo(int bar)
{
}
}
public class Foo
{
public Foo(int bar)
{
}
}
It's not like records, those actually generate properties and a bunch more code
nohopestage
nohopestage11mo ago
But here in the method the parameter can't be used as this.bar, right?
Angius
Angius11mo ago
Let's see
MODiX
MODiX11mo ago
Angius
REPL Result: Failure
public class Foo(int bar) {
public void Write() => Console.Write(this.bar);
};
new Foo(69).Write();
public class Foo(int bar) {
public void Write() => Console.Write(this.bar);
};
new Foo(69).Write();
Exception: CompilationErrorException
- 'Foo' does not contain a definition for 'bar' and no accessible extension method 'bar' accepting a first argument of type 'Foo' could be found (are you missing a using directive or an assembly reference?)
- 'Foo' does not contain a definition for 'bar' and no accessible extension method 'bar' accepting a first argument of type 'Foo' could be found (are you missing a using directive or an assembly reference?)
Compile: 453.784ms | Execution: 0.000ms | React with ❌ to remove this embed.
Angius
Angius11mo ago
It seems no, it cannot
MODiX
MODiX11mo ago
Angius
REPL Result: Success
public class Foo(int bar) {
public void Write() => Console.Write(bar);
};
new Foo(69).Write();
public class Foo(int bar) {
public void Write() => Console.Write(bar);
};
new Foo(69).Write();
Console Output
69
69
Compile: 413.586ms | Execution: 25.561ms | React with ❌ to remove this embed.
Angius
Angius11mo ago
It's fine without this, though Which, let's be honest, who ever uses this. explicitly
nohopestage
nohopestage11mo ago
Some people do, but I'm not one of them
Want results from more Discord servers?
Add your server