Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize dictionary inside class constructor

Tags:

c#

dictionary

I have a class with a property of type Dictionary<>. I am trying to initialize the property in the constructor using the following code. It gives error. Why?

Error 1

'Permissions' is a 'property' but is used like a 'type'

Code:

public class UserModel
{
    public UserModel()   // constructor
    {
        Permissions = new Permissions<Guid, List<Guid>();
    }

    public Dictionary<Guid, List<Guid>> Permissions { get; set; }
}
like image 834
immirza Avatar asked Mar 16 '26 04:03

immirza


1 Answers

You cannot create instance of property. Instead you need to create instance of a type and assign it to property.

public class UserModel
 {
    public UserModel()   // constructor
    {
        Permissions = new Dictionary<Guid, List<Guid>>();
    }
    public Dictionary<Guid, List<Guid>> Permissions { get; set; }
  }
like image 121
farid bekran Avatar answered Mar 17 '26 19:03

farid bekran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!