I want to move from Java to C# and I want to try getter and setter like in Java
internal class Person
{
internal string name;
internal void setName (string name)
{
this.name = name;
}
internal string getName ()
{
return this.name;
}
}
in main:
Person alex = new Person();
alex.setName("alex");
Console.WriteLine(alex.getName);
and the output is:
System.Func`1[System.String]
Why doesn't display the expected "alex" ?
Console.WriteLine(alex.getName())Personhas aNameand not aSetName/GetName.internalis probably also not what you want -- if the field has the same accessibility as its getter/setter, nothing prevents clients from manipulating it directly. In C# code you'd expectpublic string Name { get; set; }.getandsetprefix