views:

193

answers:

3

I have some simple code in a class:

private ITemplate _content1 = null;
[TemplateContainer(typeof(ContentContainer))]
public ITemplate Content1
{
    get
    { return _content1; }
    set
    { _content1 = value; }
}

I need about 15 of these content containers. I could just copy and paste this block 15 times and change the number, but there must be a better way. Can anyone suggest a cleaner way to do this?

+3  A: 

Try this instead:

[TemplateContainer(typeof(ContentContainer))]
public ITemplate Content1
{ get; set; }
Spencer Ruport
Note that this is only available as of C# 3.
Jon Skeet
I'm using C# 2.0 and this works for me. I double-checked my project's properties and it's definitely using 2.0. Are you sure about the 3.0 requirement?
Jon Tackabury
2.0 .Net project != C# 2.0. If it compiles you're fine.
Spencer Ruport
Crazy and awesome at the same time. It works, so I'll use it - I can deal with 2 lines per property. Thanks!
Jon Tackabury
It's a feature of the compiler in VS2008. You get the features regardless of the targeted framework.
Will Eddins
+1  A: 

There is a property (prop) Snippet (snippets are native to visual studio).

Either modify the snippet (it is a simple xml file), or create a new one for your task.

ReSharper has a easier way, called code templates.

Or, generate the properties you need with a t4 script. But that is probably overkill.

Chris Brandsma
+1  A: 

Why not use an collection of containers such as List for example? It seems that the only think you're changing is the integer index, using a List would make sense.

Paul Sasik