With C++, I can make a code as follows.
class Terminal {
int uid;
public:
void SetUid(int uid) {self.uid = uid;}
};
I tried the similar thing in C#, but I got an error. I tried the following instead, but it looks ugly.
class Terminal {
int uid;
public void SetUid(int uid_) {uid = uid_;}
}
What do you use when you want to pass a parameter that has the same name of class field variable in C#?
class Terminal {
int uid;
public void SetUid(int uid) { this.uid = uid; }
}
However, I would consider using a property instead:
class Terminal {
public int Uid { get; set; }
}
Getter and setter methods usually smell of improper C# design, since properties give you the getter/setter mechanism wrapped into a nice bit of syntactic sugar.
You can do this. Just use:
public void SetUid(int uid) { this.uid = uid; }
In C++ it's not self
, but this
, and it's actually the same in C#;
public void SetUid(int uid)
{
this.uid = uid;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With