-1

In the case where you only set a variable at the initialization of an object, but want other objects to be able to read the variable and not set it after initialization, what would be the correct syntax in C#?

From what I understood it would look something like this:

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

 public Test() {
  this.name = "Hello World!";
 }

}

Is this the correct way?

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
Zamor
  • 59
  • 1
  • 9
  • 1
    @JasonEvans: well, he's asking for the correct _syntax_, now you've corrected it ;) – Tim Schmelter Mar 01 '17 at 08:33
  • 1
    @TimSchmelter Take a look at the *pending* change :-/ – Rawling Mar 01 '17 at 08:35
  • See also e.g. https://stackoverflow.com/questions/7675205/how-to-create-a-read-only-object-property-in-c and https://stackoverflow.com/questions/13401045/how-to-make-a-field-read-only-outside-class, among many others. Oh, and the language documentation, of course. – Peter Duniho Mar 01 '17 at 08:41

5 Answers5

2

This would be the correct way, however, you may want to consider using the readonly keyword as this prevents the name variable being modified outside of the constructor.

Like so:

class Test
{
 private readonly string name;
 public string Name { get { return name; } }

 public Test()
 {
  name = "Hello World!";
 }

 public Test(string name)
 {
  this.name = name; //You use this to set scope to the object to disambiguate name
 }

}

An even simpler implementation would be to use a readonly automatic property but this doesn't prevent modification outside of the constructor:

class Test
{
  public string Name {get; private set;}

  public Test()
  {
     Name = "Hello World!";
  }

  public Test(string name)
  {
     Name = name;
  }

}

And if you're using C# 6:

class Test
{
  public string Name {get;} = "Hello World!";

  public Test()
  {
  }

  public Test(string name)
  {
     Name = name;
  }

}
toadflakz
  • 7,764
  • 1
  • 27
  • 40
1

Your implementation looks correct, but it can be expressed much simpler

public string Name {get;} = "Hello World!";
Peter Bruins
  • 807
  • 9
  • 25
  • .... with C#6 and [auto-property initializers](https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#auto-property-enhancements) – Tim Schmelter Mar 01 '17 at 08:36
0

In modern C# you can do:

class Test
{
    public string Name { get; }
    public Test(string name) 
    {
         Name = name;
    }
}

Your getter is returning the private field, but my version is using the auto backing features of .NET. Also in my version, the backing field is readonly, so you can't change it, even within in the class. If you really need that, you can use:

class Test
{
    public string Name { get; private set; }
    //...
}

There are other ways to set it, with the latest version of C#, but I prefer explicitly in the constructor to make it clear.

Jono Stewart
  • 346
  • 1
  • 2
  • 11
-1

You can use the readonly keyword - it only allows to set the value during the initialization of the class:

class Test
{
     private readonly string name;
     public string Name { get { return name; } }

     public Test
     {
         this.name = "Hello World!";
     }
}

Even methods of the same class cannot alter the value of name.

With C# 6 you can even ommit the variable name and readonly keyword:

class Test
{ 
    public string Name { get; }

    public Test()
    {
        this.Name = "my readonly value";
    }
}
Matten
  • 17,365
  • 2
  • 42
  • 64
-1

You can do the following:

class Test
{
    public readonly string Name = "Hello World!";

    public Test()
    {
    }
}

or

class Test
{
    public readonly string Name;

    public Test(string name)
    {
        this.Name = "Hello World!";
    }
}
  • 1
    Public fields are bad coding practice. – toadflakz Mar 01 '17 at 08:47
  • 1
    I don't see the difference with **public string Name {get; private set;}** in your example except that you can extend the getter and setter with additional functionality, but it depends on your need. Can you give some examples, why it is not a good practice? – Nikola Dimov Mar 01 '17 at 09:33
  • Read the answer here: http://stackoverflow.com/questions/480627/why-wont-anyone-accept-public-fields-in-c – toadflakz Mar 01 '17 at 10:02
  • 2
    http://stackoverflow.com/questions/863182/changing-fields-to-property-is-a-breaking-change-under-what-scenarios - this seams to be reasonable explanation why to use properties instead of fields, so I agree with your comment. – Nikola Dimov Mar 01 '17 at 12:54