-1

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" ?

7
  • You're missing the paranethesis "()". Console.WriteLine(alex.getName()) Commented Mar 14, 2023 at 10:39
  • 4
    Apart from the console issue, use C# properties instead. A Person has a Name and not a SetName/GetName. Commented Mar 14, 2023 at 10:40
  • 1
    Marking everything internal is 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 expect public string Name { get; set; }. Commented Mar 14, 2023 at 10:41
  • 3
    Use properties. C# isn't Java and properties are actual concepts, parts of an object's API. They aren't just getter and setter methods. Serializers and ORMs will serialize and map properties, not fields. No library will use methods that just have the get and set prefix Commented Mar 14, 2023 at 10:42
  • 1
    This is actually a frequent mistake people make in moving between languages - they assume the idioms and patterns they've picked up in a previous language are universally applicable and then try to use those in the new language. It doesn't help you learn the language if you're writing unidiomatic code that no "native" author of the new language would use. Commented Mar 14, 2023 at 10:48

2 Answers 2

3

Because getName is a function, and not a getter. So your code should be

Console.WriteLine(alex.getName());

If you want to use setter and getters, the syntax is

internal class Person
{
  internal string name;
  internal string Name 
  {
    set { this.name = value; }
    get { return this.name; }
  }
}

Or more compact

internal class Person
{
  internal string Name { get; set; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The name field shouldn't be internal. Fields are just implementation details. Only properties are part of a class's programming surface
I can see cases, not really recommended, when you want to set the field directly without passing through the setter. That's why I maintained the original Access modifiers from the question. But it is a valid note.
1

In C#, properties are used instead of explicit setters/getters. So the above is better written as

internal string Name
{
    get => this.name
    set => this.name = value;
}

or even shorter, with automatic properties:

internal string Name
{
    get;
    set;
}

Additionally, in your statement Console.WriteLine(alex.getName); getName is the method, not it's result, therefore it prints the type of the method. To fix that, change it to Console.WriteLine(alex.getName()); or, with the above property, it will be Console.WriteLine(alex.Name));

6 Comments

It's not just better, properties are part of an object's API. Fields and get-prefixed methods aren't. Serializers, ORMs, Web frameworks etc all work on properties, none on fields or methods unless explicitly configured
@PanagiotisKanavos Sure, but nevertheless (if you don't need any of that), it's just syntax. It doesn't really change the behavior when looking at the class alone.
It's not just syntax, yes, it changes behavior. Properties are specified in an assembly's metadata. You can use Type.GetProperty to get a property by name. You can specify properties in interfaces, but not fields. If you use any king of configuration or settings infrastructure, properties are used. You'd have to strip quite a lot of basic functionality to say it's just syntax.
I see it the other way round: Because we have properties, they're used in many places where it's convenient, like Serializers. But there's nothing that would prevent a library to use Type.GetField instead of Type.GetProperty. And (as in the example above), setXYZ and getXYZ methods can be part of an interface.
That's an opinion that contradicts the language design (actually, the runtime design) itself
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.