Property Default Values in C#

S

shapper

Hello,

I have a class with a few properties:

public class PageViewData {
public string ContentType { get; set; }
public string Keywords { get; set; }
public string Title { get; set; }
}

What is the correct way to define a property default value in case the
property is not defined?

Thank You,
Miguel
 
K

Kerem Gümrükcü

Hi Miguel,

there are two ways: Either initalize all values
to some default stuff in the Class Constructor,
or use attributes on the classes like e.g. this:

[CategoryAttribute("Progress Bar"),
Browsable(true),
ReadOnly(false),
BindableAttribute(false),
DefaultValueAttribute(typeof(Color),"DarkGreen"),
DesignOnly(false),
DescriptionAttribute("The Progress Bar Color that will be used to
draw the Progress Bar")]
public Color ProgresBarColor {

get {

return this.colorProgressBar;
}

set {

this.colorProgressBar = value;
this.progressBarBrush = new
SolidBrush(this.colorProgressBar);
}

}

its a piece of code that i use in my extended
progress bar class. See the .NET Documentation
for more information about attributes and the
information about the classes in this example,...

Regards

Kerem

--
 
J

Jon Skeet [C# MVP]

Hello,

I have a class with a few properties:

  public class PageViewData {
    public string ContentType { get; set; }
    public string Keywords { get; set; }
    public string Title { get; set; }
  }

What is the correct way to define a property default value in case the
property is not defined?

You can't do it directly using automatically implemented properties -
other than by setting the properties in the constructor(s) of course.
There are a few things like this where automatic properties could be
made more pleasant fairly easily. It would be nice to see them tweaked
for C# 4, but I don't expect that to happen. Maybe for C# 5 -
particularly as part of a push towards making immutability easier.

Jon
 
A

Alberto Poblacion

shapper said:
I have a class with a few properties:

public class PageViewData {
public string ContentType { get; set; }
public string Keywords { get; set; }
public string Title { get; set; }
}

What is the correct way to define a property default value in case the
property is not defined?

Use the "old" syntax, and assign the default value to the private
variable that stores the property:

private string contentType="defaultValue";
public string ContentType
{
get { return contentType; }
set { contentType = value; }
}
 
M

Marc Gravell

Either initalize all values
to some default stuff in the Class Constructor,
or use attributes on the classes

Attributes won't change the value, though - they are used mainly to
determine whether a value should be saved, and to reset the value
(akin to ShouldSerialize{name} and Reset{name}). Generally you need
code in the ctor, or a field initializer (which you can't do with auto-
implemented properties).

Marc
 
S

shapper

   Use the "old" syntax, and assign the default value to the private
variable that stores the property:

private string contentType="defaultValue";
public string ContentType
{
   get { return contentType; }
   set { contentType = value; }

}

I see, so it is exactly the same as in VB.NET. I though, since C# has
automatic properties, that this would be different.

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Property Default Value 3
Override property 18
Intersection of Lists 5
Interface in Property 4
Property 7
HttpPostedFileBase and byte[] 9
Property and Global Variable 2
Fill Dictionary in a Fluent Way 4

Top