Jona Pie
Jona Pie

Reputation: 83

How to access value from a static C# list

I have created a static class which contains some static values list. Now I want to access these values out side from this class. I need to know how can I do this? The code example is provided below:

public class RoleList
    {

        static public List<RoleDetials> Roles()
        {
            List<RoleDetials> roleDetaildsList = new List<RoleDetials>();
            roleDetaildsList.Add(new RoleDetials
            {
                Id = 1,
                Name = "admin"
            });
            roleDetaildsList.Add(new RoleDetials
            {
                Id = 2,
                Name = "sadmin"
            });
            roleDetaildsList.Add(new RoleDetials
            {
                Id = 3,
                Name = "badmin"
            });

            return roleDetaildsList;
        }


    }


    public class RoleDetials
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

I already tried to like below but this way not works as per C# syntax.

var allRoles = RoleList.ToList();

Upvotes: 0

Views: 91

Answers (3)

Nasty35
Nasty35

Reputation: 31

To access to data is

List<RoleDetials> allRoles = RoleList.Roles();

And replace

static public List<RoleDetials> Roles()

For

public static List<RoleDetials> Roles()

Upvotes: 2

Sajeetharan
Sajeetharan

Reputation: 222542

You need to call the method as,

List<RoleDetials> roles = RoleList.Roles();

Upvotes: 0

Swift
Swift

Reputation: 3410

You call the static method with

var allRoles = RoleList.Roles();

Upvotes: 2

Related Questions