How to set a default value of a property on every object that implements an interface?

I have a property called Color on my IVehicle interface. If I want every vehicle that implements that interface to have a default color of "Red" how could I achieve this? Do I need another level here?

public interface IVehicle
{
    string Color { get; set; }

    void Go();        
    void Stop();
}

public class Bmw : IVehicle
{

    #region IVehicle Members

    public string Color
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public void Go()
    {

    }

    public void Stop()
    {

    }

    #endregion
}

Answers


If I want every vehicle that inherits from that interface to have a default color of "Red" how could I achieve this?

This would really be an implementation detail, so it's not part of the interface. The interface can't dictate how a class implements it - only what members must be implemented.

If you want this type of control, you might want to use an abstract class instead of an interface. A Vehicle base class could setup the default implementation using Colors.Red.


Need Your Help

extract content after pattern matching in linux

linux bash shell awk

I want to extract some content after a matching pattern from a file.

Locking Output to Output Window in Visual Studio

c++ visual-studio visual-studio-2008 winapi trace

My application sends debug data to the Output Window in Visual Studio using the TRACE macro. Some of these output strings are built using several calls to the TRACE macro, as is shown in the follo...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.