C
C#10mo ago
cheeze2000

✅ Don't quite understand the `required` keyword

although i marked the member as required, i defined a default value in case if it's not in the object initializer. does the keyword not work this way?
No description
8 Replies
Angius
Angius10mo ago
Makes the property required Unconditionally If you're fine with a default value, make it not required (also, make it a property)
cheeze2000
cheeze2000OP10mo ago
by property, you mean the get set stuff?
Angius
Angius10mo ago
Yes The general rule of thumb is "if it's public, it should be a property"
cheeze2000
cheeze2000OP10mo ago
oh i see then what should i be using members?
Angius
Angius10mo ago
Fields are usually private If that's what you mean by "member" $structure
MODiX
MODiX10mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
Angius
Angius10mo ago
Here's most of the naming
cheeze2000
cheeze2000OP10mo ago
nice thanks

Did you find this page helpful?