开发者

Interface with getter and setter in c#

As I read here http://msdn.microsoft.com/en-us/library/75e8y5dd%28v=VS.100%29.aspx

It is possible to have get in an Interface BUT NOT set ?

OR if I want getter and setter in Interface, do I have to use the old syntax开发者_StackOverflow社区 getVar setVar just because new syntax doesn't fit Interface syntax?

Update: If I must omit set in Interface, does this means I cannot enforce class to have setter which defeats the purpose of having an Interface in this case as I can only partially enforce?


No. I think you misunderstood. That article is about the possibility of having an interface with a readonly property (a property with only getter). But, if you need, you can put also the setter in the interface:

interface IHasProperty
{
    string Property{ get;set; }
}
class HasProperty:IHasProperty 
{
    public string Property{ get;set; }
}


You can use property syntax. Use this combination:

interface ISomething
{
    string Test { get; }
}

class Something : ISomething
{
    public string Test { get; private set; }
}

You can of course add full implementations for the getters in Something.Test, if you choose to. I only used backing fields for brevity.

Remember that an interface defines the bare minimum set of things you must implement. You can add any gravy (new methods, accessors, members, etc) on top that you want. You could even add a public setter:

interface ISomething
{
    string Test { get; }
}

class Something : ISomething
{
    public string Test { get; set; } // Note that set is public
}

The only restriction is that someone can't use the gravy you add, unless they have a reference of the concrete type (the class, not the interface), or a different interface that defines the methods you added.


Yes, just omit set; from the property declaration. For example:

interface IName
{
    string Name { get; }
}


The answer in fact is the mixture of the above answers: omitting setter on the interface and having get; private set; on the class.


If you only want the get available just use {get;private set;}

http://msdn.microsoft.com/en-us/library/bb384054.aspx

The class that is shown in the previous example is mutable. Client code can change the values in objects after they are created. In complex classes that contain significant behavior (methods) as well as data, it is often necessary to have public properties. However, for small classes or structs that just encapsulate a set of values (data) and have little or no behaviors, it is recommended to make the objects immutable by declaring the set accessor as private. For more information, see How to: Implement a Lightweight Class with Auto-Implemented Properties (C# Programming Guide).

Attributes are permitted on auto-implemented properties but obviously not on the backing fields since those are not accessible from your source code. If you must use an attribute on the backing field of a property, just create a regular property.


You misunderstood. According to the article you cannot use access modifiers on interface.

You CAN use both get and set in interface property!

See in the following MSDN example:

http://msdn.microsoft.com/en-us/library/87d83y5b(v=VS.100).aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜