views:

99

answers:

4
public class MyClass
{
  public string Name {get; KEYWORD set;}

  public MyClass(string name)
  {
    this.Name = name;
  }
}

Any ideas what the KEYWORD was? I searched all over but it's hard to find the get/set accessors in google.

+7  A: 

The private keyword allows the property to be set from anywhere within the class:

private set;

I.e:

public string Name {get; private set;}

If you also wanted to allow it to be set from an inheriting class, you could use protected instead.

You could use the readonly keyword to only allow the property to be set once, but it cannot be used on auto-implemented properties.

GenericTypeTea
Thanks I did it but get this:Error 1 The accessibility modifier of the 'MyClass.Name.set' accessor must be more restrictive than the property or indexer 'MyClass.Name'
Joan Venge
You're getting the error, probably, because your property isn't public.
Daniel Straight
How is MyClass.Name declared? I'm guessing it's not public string
Bob Kaufman
that error sounds like you have it flipped: private string Name {get; public set;} would say "hey, public set isn't more restrictive than private Name." If you have the property as public but the set as private, you shouldn't see that error message.
Kate Gregory
Private still allows it to be reset else ware in the same class. The question specifically states that it is to be set in the constructor only.
chilltemp
Thanks, that was it. So does that mean, if the property itself is private, there is no need to use PRIVATE SET? But I still want to prevent any code in the class to reassign the value of the Name property.
Joan Venge
@Joan Venge, the only way to prevent any other part of this class setting the field is to use the `readonly` keyword.
Darin Dimitrov
+10  A: 

If you want to be able to set the value only in the constructor I would recommend you the readonly keyword:

public class MyClass
{
    private readonly string _name;
    public MyClass(string name)
    {
        _name = name;
    }

    public string Name 
    {
        get { return _name; }
    }
}
Darin Dimitrov
Thanks, this can't be done using auto properties, right?
Joan Venge
No, unfortunately it can't, that's why you have the `readonly` keyword :-)
Darin Dimitrov
Thanks Darin. Shame they didn't allow this for auto properties. Because it could be possible to do for auto properties by Microsoft, right?
Joan Venge
@Joan, many things are possible but as Eric Lippert has written in his blog in order to implement a feature there must be someone to write the specification, implement, test, document and ship which costs time and money and there are priorities and deadlines that has to be respected. Quote: ` And therefore someone had to analyze the problem space, weigh the costs and benefits of possible approaches, design a solution, implement a solution, test it, document it and ship it to customers. No magic involved!` (http://blogs.msdn.com/b/ericlippert/archive/2009/03/20/it-s-not-magic.aspx)
Darin Dimitrov
Thanks Darin. Just wanted to be sure so it's not theoretically possible. But I see what you mean.
Joan Venge
+2  A: 

I know that using the READONLY keyword on a field let you write in this field once and it must be only in the constructor.

iChaib
+1  A: 

like GenericTypeTea said you are probably looking for the private keyword.

However, just to be clear, making your set accessors private doesn't restrict it to ONLY the constructor but anyway within the class.

To make the property ONLY settable from the constructor you want something like this

public class MyClass
{
  private string name;
  public string Name {get { return name;} }

  public MyClass(string nameString)
  {
    name = nameString;
  }
}
Foovanadil