Roman
Roman

Reputation: 763

Is there any way to simplify field initialization?

So I was wondering if I can simplify this somehow as it's taking a lot of space on screen:

private Dictionary<string, Dictionary<string, List<double>>> storage = new Dictionary<string, Dictionary<string, List<double>>>();

Upvotes: 0

Views: 105

Answers (2)

user1023602
user1023602

Reputation:

Create a new class derived from the long-winded datatype, and use that instead.

public class NumberStorage : Dictionary<string, Dictionary<string, List<double>>>
{
}

and then

private NumberStorage storage = new NumberStorage();
  • Aliases (the using keyword) must be declared in each file.

  • The advantage of creating a new class is that it only has to be done once.

Upvotes: 2

Furkan Kambay
Furkan Kambay

Reputation: 760

You can use an alias for it.

using System;
// and others
using MyDict = System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<double>>>;

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main()
        {
            var a = new MyDict();
        }
    }
}

or you can do it like this:

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    using MyDict = Dictionary<string, Dictionary<string, List<double>>>;

    public class Program
    {
        public static void Main()
        {
            var a = new MyDict();
        }
    }
}

Upvotes: 7

Related Questions