1

I have a C# class with properties in which I end up using this class a a collection of type List in another class.

What I want to do is just always set the Type property to be of value "3"

Should /Can this be done with the getter/setter or should I use the System.Component.DefaultValue .... attribute

public class ReportDefinition 
{

    public int Id { get; set; }
    public string ReportGroupNameDef { get; set; }
    public int SortOrder { get; set; }
    public int ReportGroupId { get; set; }

    [System.ComponentModel.DefaultValue(3)]
    public int Type { get; set; }

}

I think that I would prefer not using this way [System.ComponentModel.DefaultValue(3)]

  • 2
    If [c# 6 or higher](http://stackoverflow.com/questions/40730/how-do-you-give-a-c-sharp-auto-property-a-default-value): `public int Type { get; set; } = 3;` – Quantic Mar 25 '16 at 20:57
  • c# 6 is .net 4.5 ? –  Mar 25 '16 at 20:58
  • well, my Linqpad complains about it... Linqpad 4 .... what other way ? –  Mar 25 '16 at 20:59
  • I do like to do POC in linqpad, but I do end up putting code into VS 2015 –  Mar 25 '16 at 21:02
  • LinqPad 5 supports it, and no, C# 6 is a roslyn compilation feature. Language version is not dependent on .net version. – Dispersia Mar 25 '16 at 21:08
  • 1
    Right, i hear you - While I'm using VS 2015 90% of the time, still have too many other applications with developers on VS 2013 , and I have not bought the Linqpad 5.... sure free version is there but limited... I bought linqpad 4 years ago... I seem to spend 50% of my time on the frontend with javascript and all sorts of frameworks... so I guess for me to really dig into all the C# 6 new features simply has not been on my priority list of things to currently learn –  Mar 25 '16 at 21:13

2 Answers2

0

You could use a read-only property, and either return the value of a private field, or just return the value you want right in the get.

public class ReportDefinition
{
  private int m_type = 3;
  public int Type 
  { 
    get 
    {
      return m_type; 
    }
  }
}
Zack
  • 2,789
  • 33
  • 60
  • You could also consider using an enum instead of a [Magic Number](http://www.c-sharpcorner.com/uploadfile/GemingLeader/refactoring-magic-numbers/) if that is what `3` represents. – Zack Mar 25 '16 at 21:03
  • What is wrong with this answer that someone downvoted it. I really think people should be required to leave a comment on any downvote, even if you wish to be set as anonymous –  Mar 25 '16 at 21:04
  • 1
    This works as I have 3 classes of which the main class I need to know that it is the main thus it will get Type = 1 , then another class with properties having Type set to 2 , another to Type of 3 , I'm representing database tables that don't have obvious ways to know when the data is sent out to kendo / javascript , then a ajax call is made with the data and its children items, i need to know which set of data and children belong to a specific table . I explain all that here as I fail the pain of Zach being downvoted. I have not yet committed myself to C# 6 , so thus this seems good. –  Mar 25 '16 at 21:09
0

I don't expect to get or steal away the existing selected answer but I did want to help clarify a bit of the comments that I am reading.

  1. Yes indeed best clean way is auto-property public int Type { get; } = 3; -- Caveat is C# 6

  2. Another C# 6 without auto-property would be an expression body

    private int m_type = 3;
    public int Type => m_type;   
    
  3. However If you are stuck with wanting to use Linqpad 4 then the selected answer is "fine"

    private int m_type = 3;
    public int Type 
    { 
      get 
      {
          return m_type; 
      }
    }
    
Tom Stickel
  • 19,633
  • 6
  • 111
  • 113