C
C#3w ago
stigzler

Sloppy Constructors

I know I'm missing something here. There's likely a better way of doing the below?
public class SolidColorBrush : Brush
{
public Color Color { get; set; } = System.Drawing.Color.White;

public SolidColorBrush()
{
BrushType = Enums.BrushType.Color;
}

public SolidColorBrush(Color color)
{
Color = color;
BrushType = Enums.BrushType.Color;
}
}
public class SolidColorBrush : Brush
{
public Color Color { get; set; } = System.Drawing.Color.White;

public SolidColorBrush()
{
BrushType = Enums.BrushType.Color;
}

public SolidColorBrush(Color color)
{
Color = color;
BrushType = Enums.BrushType.Color;
}
}
6 Replies
blueberriesiftheywerecats
public SolidColorBrush(Color color) : this()
{
Color = color;
}
}
public SolidColorBrush(Color color) : this()
{
Color = color;
}
}
Or just override BrushType
Krishna
Krishna3w ago
public class SolidColorBrush : Brush
{
public Color Color { get; }

public SolidColorBrush() : this(System.Drawing.Color.White)
{
}

public SolidColorBrush(Color color)
{
Color = color ?? throw new ArgumentNullException(nameof(color), "Color cannot be null.");
BrushType = Enums.BrushType.Color;
}
}
public class SolidColorBrush : Brush
{
public Color Color { get; }

public SolidColorBrush() : this(System.Drawing.Color.White)
{
}

public SolidColorBrush(Color color)
{
Color = color ?? throw new ArgumentNullException(nameof(color), "Color cannot be null.");
BrushType = Enums.BrushType.Color;
}
}
Doing so can improve below: 1. Immutability - The Color property is now read-only (get; only), making the class safer and easier to work with in multi-threaded or complex scenarios. 2. Constructor Chaining: - The parameterless constructor calls the main constructor with the default Color.White. This eliminates duplicate logic and makes it easier to maintain. 3. Validation: - A check ensures that the color is not null, preventing potential errors. 4. Simplified Initialization: - The logic to set the default color and brush type is centralized in the constructors.
:', ryte ,':
:', ryte ,':2w ago
System.Drawing.Color is a struct, is this ai generated?
SleepWellPupper
Please refer to the $rules Specifically:
Relaying ChatGPT/AI generated answers or sending Let Me Google That For You links is heavily discouraged, use of either may not be tolerated.
SleepWellPupper
Sorry about the reaction image, I assumed that tag produced the rules list.
Want results from more Discord servers?
Add your server