5

I'm a newbie and I'm trying to learn the basics of C#. This might sound quite trivial and may be stupid but its a doubt. While going through one of the source codes of an application, I saw a piece of code inside a class

private string fname;
public string FirstName
{
    get
    {
       return fname
    }
    set
    {
       fname = value;
    }
}

Can anyone tell me what it means. I understand that when we declare a class we access fname using an alias FirstName. If it's for some security purpose then what?

psubsee2003
  • 8,563
  • 8
  • 61
  • 79
iJade
  • 23,144
  • 56
  • 154
  • 243
  • 1
    Take a look at an article about properties - http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx – alex Mar 17 '13 at 16:52
  • 3
    I think it's a little off that iBlue should have a down vote for not knowing this and simply asking... – Paul Mar 17 '13 at 16:55
  • @Westie title was not the best, that tends to attract quick downvotes. – psubsee2003 Mar 17 '13 at 16:57
  • possible duplicate of [properties in C#](http://stackoverflow.com/questions/14559909/properties-in-c-sharp) – Roman C Mar 18 '13 at 09:05

5 Answers5

6

This code is also equivalent to:

public string FirstName { get; set; }

What this do is define a property. In C# properties provide encapsulation for private fields.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    As a follow up, this also provides a location to add validation (like prevent first name from being assigned an empty value, make sure it's always lowercase, etc.) – Brad Christie Mar 17 '13 at 16:55
  • But how does the compiler know that `FirstName` is tied to `fname`? Thanks. – Sabuncu May 09 '16 at 20:40
  • what is the difference than having only public string FirstName; – RollRoll Jun 06 '16 at 02:28
  • Also you can specify that the property is read-only to the public by saying only `{ get; }`, and still be able to set it inside the class. – JonP Apr 14 '22 at 09:32
3

You can write your custom logic on your property. F.e, some validation:

public string FirstName
{
    get
    {
       return fname;
    }
    set
    {
       if (value.Count(s => Char.IsDigit(s)) > 0)
       {
           throw new Exception("Only letters allowed");
       }
       fname = value;
    }
}
Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
  • You get the point across but lets remind people that you don't need to invoke `ToCharArray` to call `IEnumerable` methods. – ChaosPandion Mar 17 '13 at 17:02
  • OK...i get dat but how do u implement the above code if its like this public string FirstName { get; set; } – iJade Mar 17 '13 at 17:04
  • @iBlue There is not difference with simple property and field. Also you can look at this: http://stackoverflow.com/questions/2374416/why-use-simple-properties-instead-of-fields-in-c – Farhad Jabiyev Mar 17 '13 at 17:07
2

fname is a field and has private visibility but FirstName is a public property therefore it will be visible outside of the class and can contain logic inside get and set methods

syned
  • 2,201
  • 19
  • 22
0

It's called Properties (MSDN article). The reason for using them is to encapsulate accessing some class field to be able to easily change class behavior in future if needed.

This is also equivalent to so called auto-property, since the property at this moment oftimedoes not add any logic:

public string FirstName { get; set; }
J0HN
  • 26,063
  • 5
  • 54
  • 85
0

get and set methods are called accessors(getters) and mutators(setters) these methods are used to access and mutate the attributes of an object without allowing the access from outside the class. See that access modifier of the variable fname is private which means it can only be accessed by any method inside the class.

and note that the get and set methods should normally be given the public access modifier which enables the method to be accessed from any outside class.

direndd
  • 642
  • 2
  • 16
  • 47