0

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();
3
  • 2
    Did you forgot to Call Roles()? For example RoleList.Roles() Commented Jan 27, 2019 at 11:24
  • 1
    static public List<RoleDetials> Roles() { ... } is not static? Commented Jan 27, 2019 at 11:25
  • 1
    All answers so far are correct. What they do not explain is that, contrary to your conception, there is no static List in your code. There is a static method that returns a List when called. — Also, as a spelling nitpick, it should be 'RoleDetails' instead of 'RoleDetials'. Commented Jan 27, 2019 at 11:53

3 Answers 3

2

You call the static method with

var allRoles = RoleList.Roles();
Sign up to request clarification or add additional context in comments.

Comments

2

To access to data is

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

And replace

static public List<RoleDetials> Roles()

For

public static List<RoleDetials> Roles()

1 Comment

As long as static comes before the return type it's placement is a matter of style only.
0

You need to call the method as,

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.