C# Interface to create Optional property -
i have interface written in c# , implemented classes. possible can add 1 more property optional in interface , without modifying exiting implemented classes?
e.g
public interface ishape { int area { get; } } public class findwindow : ishape { public int area { { return 10; } } }
in findwindow written. possible can add 1 optional property , not implementing in existing class.
ie,
public interface ishape { int area { get; } //optional //string windowname{get;} } public class findwindow : ishape { public int area { { return 10; } } //windowname not implementing here } public class findwindowname : ishape { public int area { { return 20; } } public string windowname { { return "stack overflow"; } } }
there's no concept of optional members of interface.
what need here define 2 interfaces. 1 contains area
, , other contains windowname
. this:
public interface ishape { int area { get; } } public interface ifindwindow: ishape { string windowname { get; } }
then implementing classes can choose implement either ishape
or ifindwindow
.
at runtime use is
operator determine whether or not ifindwindow
implemented object @ hand.
ishape shape = ...; if (shape ifindwindow) ....
and use more derived interface use as
operator:
ishape shape = ...; ifindwindow findwindow = shape ifindwindow; if (findwindow != null) string windowname = findwindow.windowname;
Comments
Post a Comment