5 Replies - 396 Views - Last Post: 29 September 2012 - 10:59 AM Rate Topic: -----

#1 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

automatic properties read only question

Posted 28 September 2012 - 09:13 AM

Im reading pro c# 2010 by andrew troelsen and he said that automatic properties can't be buils as read only or write only

Quote

Unlike traditional C# properties, however, it is not possible to build read-only or write-only
automatic properties. While you might think you can just omit the get; or set; within your property
declaration as follows:
// Read-only property? Error!
public int MyReadOnlyProp { get; }


thats what he said. But how did System.Exception got away with it?
 public virtual string Message { get; }


This post has been edited by tlhIn`toq: 28 September 2012 - 02:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: automatic properties read only question

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: automatic properties read only question

Posted 28 September 2012 - 09:43 AM

It doesn't. The System.Exception.Message property isn't an automatic property. It's defined like this:

public virtual string Message
{
        get
        {
            if (this._message != null)
            {
                return this._message;
            }
            if (this._className == null)
            {
                this._className = this.GetClassName();
            }
            return string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Exception_WasThrown"), new object[] { this._className });
        }
}


Was This Post Helpful? 2
  • +
  • -

#3 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: automatic properties read only question

Posted 28 September 2012 - 09:55 AM

I checked MSDN but that didn't show up. Thank you so much

Where did you get that? I tried typing "System.Exception.Message c#" in google but none of the result seemed right
Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: automatic properties read only question

Posted 28 September 2012 - 11:50 AM

No problem. To get that code, I decompiled the System.Exception class using the standard version of .NET Reflector.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3450
  • View blog
  • Posts: 10,646
  • Joined: 05-May 12

Re: automatic properties read only question

Posted 29 September 2012 - 07:09 AM

I usually do this to get the "readonly" effect:
class Person
{
    public DateTime BirthDate { get; private set; }
}


Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: automatic properties read only question

Posted 29 September 2012 - 10:59 AM

If you think about it, not being able to make it read or write only makes sense. How would you set a read only property? What's the point of a write only property? Skydiver has the right solution, use the private keyword if you won't want outside code reading/writing the property.

This post has been edited by Momerath: 29 September 2012 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1