u/2308482093842 - Archived Voat Post in v/programming
u/2308482093842
  • home
  • search

u/2308482093842

0 posts · 1 comment · 1 total

Active in: v/programming (1)

  • ‹‹‹
  • ‹‹
  • ‹
  • 1
  • ›
  • ››
  • ›››
Comment on: WPF C#: Static classes, when to use them?

Static classes can sometimes make sense, but often they are just a quick and dirty hack and they make your code harder to extend in the future.

Only if there's no way in hell that you could ever use a second instance of something, only then a static class makes sense.

But 99.9% of the time, you can actually Benefit from allowing more than one instance.

Often, beginners initially like static classes, because you don't have to pass the objects to all your other objects, which is tedious.

But there is a middle Ground:

public class ChannelModel
{
    public static ChannelModel Default { get; } = new ChannelModel();
    ...
}
public class ViewModel
{
    ChannelModel ChannelModel { get; set; }
    public ViewModel(ChannelModel ChannelModel = ChannelModel.Default)
    {
        this.ChannelModel = ChannelModel;
    }
}

Now you have your "static" instance that you don't have to pass around, but you can have multiple instances, since the class is not static.

0 21 Jun 2018 10:27 u/2308482093842 in v/programming
  • ‹‹‹
  • ‹‹
  • ‹
  • 1
  • ›
  • ››
  • ›››

archive has 9,592 posts and 65,719 comments. source code.