Reputation: 161
Since we can declare a C# property as
class A
{
private int x;
public int X
{
get
{
return x;
}
set
{
x = value;
}
//Create methods in here
}
...
}
Is it possible to create methods in these properties such that you can call the method directly?
A a = new A();
a.X.SomeMethod(); //Do something with X
Instead of
A a = new A();
A.SomeMethod(a);
Upvotes: 1
Views: 1223
Reputation: 12638
No, this is not possible in C#, yet the CLI specifies .other
methods.
Properties are used to support the OOP principle of encapsulation. They can be used as if they are public data members, but they are actually special methods called accessors.
public String S;
can be represented with a property the following:
public String S { get; set; }
This is just the short-hand version for
public String S { get { return s; } set { s = value; } }
Within get { }
and set { }
you can do validations, etc.
Within a property, only get and set are allowed as methods (called accessors)
Accessing a public member via a property
myVar.S // of type string
enable you to call all methods string allows, like
string sub = myVar.S.Substring(1);
Upvotes: 0
Reputation: 4218
Is it possible to create methods in these properties such that you can call the method directly?
On the one hand, yes it is possible and you can, but on the other hand, not in c#.
Standard ECMA-335, Common Language Infrastructure (CLI)
section II.17 clearly states:
A property can contain any number of methods in its body.
That means you can only create up to one .get
and .set
method, but as many .other
methods as you like.
This example of a property called count can you find in II.17 too:
.class public auto autochar MyCount extends [mscorlib]System.Object {
.method virtual hidebysig public specialname instance int32 get_Count() {
// body of getter
}
.method virtual hidebysig public specialname instance void set_Count(int32 newCount) {
// body of setter
}
.method virtual hidebysig public instance void reset_Count() {
// body of refresh method
}
// the declaration of the property
.property int32 Count() {
.get instance int32 MyCount::get_Count()
.set instance void MyCount::set_Count(int32)
.other instance void MyCount::reset_Count()
}
}
Since c# dont know the concept of .other
methods, you will not be able to create such a method nor call it directly. Calling it is only possible through reflection.
Upvotes: 1
Reputation: 29244
If the return type is a built in type (and not a custom class) you can create an Extension Method
for all of this type.
Try this code:
public class A
{
public int X { get; set; }
}
static class Extensions
{
public static bool IsEven(this int x)
{
return x%2==0;
}
}
class Program
{
static void Main(string[] args)
{
var a=new A() { X=32 };
if (a.X.IsEven())
{
}
}
}
Upvotes: 0
Reputation: 2460
You can call method only from its object
, in your case you cannot call the method from int
, what you can do is, inherit
from int
class and create the method inside your class and put your properties type to your new created class and call the method
public class NewType : int
{
public void SomeMethod()
{
}
}
class A
{
private NewType x;
public NewType X
{
get
{
return x;
}
set
{
x = value;
}
}
}
And than you can do the follow:
A a = new A();
a.X.SomeMethod();
Hope helps
Upvotes: 1