I will modify your question a bit to provide a better comparison. In Java, you usually have public getters and private setters, and the constructor is a parameter of the initialicor [sic] variable, for example:
public class Person{ private String fName; public Person (String name) { setName(name); } private void setName(String someName){ fName = someName; } String getName(){ return fName; } }
When the class user can get the value after initialization through the constructor:
public class Example { Person person = new Person("Fred"); System.out.println(person.getName());
Now, C # can be confused here, because instead of using Person.getName you just use a variable, but this variable can still be encapsulated. In Java, you are taught that class variables must be local (private) and should only be accessible using getters and setters. C # is essentially the same, but the syntax and logic are different. Rewriting my example in C # would be:
public class Person { public String fName {get; private set;} public Person(String name) { this.fName = name; } } public class Example { Person person = new Person("Fred"); Console.WriteLine(person.fName);
Now, if you want to add logic to your getter and setter using the above conventions, you will need to enter a local private variable, but the code in Example and your Person constructor will not change:
class Person { private String _fName; public String fName { get { return _fName + ".addedText"; } private set { _fName = value.ToLower(); } } public Person(String fName) { this.fName = fName; } }
Now whether this is better or worse than Java is somewhat debatable, but from what I saw your code would be inappropriate in C # if you do something like below, although the syntax is wise it will work:
class Person2 { private String fName; public Person2(string fName) { setFname(fName); } private void setFname(String fName) { this.fName = fName.ToLower(); } public String getFname() { return this.fName+ ".addedText"; } }