Jonathan S
Jonathan S

Reputation: 155

Shortcut when creating a new class in C#

Is it possible to clean up the following code in any way?

This is what I use in my code:

var _index = new IndexViewModel();
_index.PageMeta = new PageMeta();
_index.Que = new Q();
_Index.Test = new T();

This is my class definition:

public class IndexViewModel {
   public PageMeta PageMeta { get; set; }
   public T Test { get; set; }
   public Q Que { get; set; }
}

I'm trying to remember if there's a shortcut when declaring a new class. Would it be possible for me to put something in the constructor of IndexViewModel ?

Upvotes: 2

Views: 313

Answers (5)

stuartmclark
stuartmclark

Reputation: 1243

I think you could do:

var _index = new IndexViewModel() 
{ 
    PageMeta = new PageMeta(),
    Test = new T(),
    Que = new Q()
};

Upvotes: 1

Maximilian Mayerl
Maximilian Mayerl

Reputation: 11357

You mean you want a shortcut to initialize the instance, right? If so:

var _index = new IndexViewModel()
{
    PageMeta = new PageMeta(),
    Que = new Q(),
    Test = new T()
};

Reading your comment: I think I didn't understand your question correctly. You want this initialization to happen for EVERY instance, right?

public class IndexViewModel 
{
    public PageMeta PageMeta { get; set; }
    public T Test { get; set; }
    public Q Que { get; set; }

    public IndexViewModel()
    {
        this.PageMeta = new PageMeta();
        this.Test = new T();
        this.Que = new Q();
    }
}

Upvotes: 1

Sergi Papaseit
Sergi Papaseit

Reputation: 16184

Well, you could use an object initializer:

var _index = new IndexViewModel 
{
    PageMeta = new PageMeta(),
    Que = new Q(),
    Test = new T()
};

Which might not be much of a shorcut but does save you some bit of repetitive code.

EDIT to clarify question on comment

public class IndexViewModel {

   // The default constructor now initializes de values:
   public IndexViewModel() 
   {
       PageMeta = new PageMeta();
       Q = new Q();
       T = new T();
   }

   public PageMeta PageMeta { get; set; }
   public T Test { get; set; }
   public Q Que { get; set; }
}

If you add initialization to the constructor you don't need to use an object initializer anymore:

var _index = new IndexViewModel(); //<- initialized by its own constructor

Upvotes: 5

yas4891
yas4891

Reputation: 4862

from (I think)C# 3.0 onwards you could do:

var _index = new IndexViewModel { 
                   PageMeta = new PageMeta(), 
                   Que = new Q(), 
                   Test = new T()
};

Upvotes: 0

Jared Updike
Jared Updike

Reputation: 7277

You could easily add a constructor that creates each of those new objects by default:

public class IndexViewModel {
   public IndexViewModel() {
       PageMeta = new PageMeta();
       Q = new Q();
       T = new T();
   }
   public PageMeta PageMeta { get; set; }
   public T Test { get; set; }
   public Q Que { get; set; }
}

Then when you need a new IndexViewModel with a new one of each of those objects, just call

new IndexViewModel()

Upvotes: 2

Related Questions