I’m looking at some code right now, and I am a little confused. This class is not an interface, but why those methods are not defined? Also, bedsides method signatures, there is a regular property. Very confusing!
public class MyClass
{
public string Foo(string str1);
public string Bar(string str, int i);
public string myProperty { get; set; }
}
Thanks for helping
No, that is not valid as shown, and you could prove as much by trying to compile it. For a class to contain unimplemented methods it must be declared as
abstract, and the same applies to each unimplemented method.abstractclasses are conceptually similar to an interface, but they may contain implementation as well. This makes them useful for scenarios in which some methods may share a common implementation all the way down the inheritance hierarchy, but others only have meaning when implemented by a descendant type.Methods marked as
externorpartialcan also lack a body, butexternmethods are typically interop (so it is implemented in native code), andpartialmethods will be implemented in another.csfile (and can be declared only in a class marked aspartial).As an aside, the property is fine because it is automatic (syntactical sugar which means that the backing field will be created for you and the get/set methods simply return/set that field, nothing more).