0
private bool duplicate;
public bool duplicateNumber
{
    get
    {
        return duplicate;
    }
    set
    {
        duplicate = value;
    }
}

What does this snippet mean? How to use it? I am new on c# and know java only. the variable value doesn't need to be declared seems...

Why don't just make a getter or setter for a private variable?

Paul Tyng
  • 7,924
  • 1
  • 33
  • 57
TheOneTeam
  • 25,806
  • 45
  • 116
  • 158

3 Answers3

7

I recommend reading on Properties

Toni Parviainen
  • 2,217
  • 1
  • 16
  • 15
  • What does the benefit ? i can make a getter or setter for a private variable..? – TheOneTeam Feb 16 '12 at 06:40
  • @KitHo The benefit is that you might have additional logic in you setter. If you only need the property you can use [automatic properties](http://msdn.microsoft.com/en-us/library/bb384054.aspx) in which case you don't need the private field at all – Toni Parviainen Feb 16 '12 at 15:54
  • The link has a private field with a public property, that is the purpose. You could also add logic, for example a `FullName` property could concatenate the values of the `FirstName` and `LastName` properties. – Paul Tyng Feb 16 '12 at 20:42
  • While this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – jjnguy Feb 22 '12 at 03:02
2

Here's a link to MSDN's Properties

http://msdn.microsoft.com/en-us/library/aa288470%28v=vs.71%29.aspx

Quoting:

Note that in a property Set method a special value variable is available. This variable contains the value that the user specified, for example:

myName = value; 
Alex
  • 7,901
  • 1
  • 41
  • 56
0

your duplicateNumber is property. When u assign it like

duplicateNumber = false;

it becomes like

set
{
      duplicate= false;
}

where value is the value passed to the property.

when u fetch that property like

bool xyz = duplicateNumber;

this is called

get
{
    return false;
}

where false is the value of duplicate variable.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208