0
public class Role
{
    public string RoleName { get; set; }
    public int RoleId { get; set; }

    public Role(int roleid, string roleName)
    {
        RoleName = roleName;
        RoleId = roleid;
    }
}

public class RoleManagement
{

    public List<Role> RoleList = new List<Role>();
    RoleList.Add(1, "Software Engineer");
}

I am trying to add some values to the list

I am facing a errors such as

  1. Type Expected

  2. Tuple must contain atleast two elements

How can i add some elements into the list

1
  • C# is a language of types. What is the type of RoleList? What is the type of the parameter(s) to RoleList.Add()? What type(s) are you passing? How can you create an object of the correct type(s) to pass to RoleList.Add()? Commented Dec 5, 2022 at 22:01

1 Answer 1

5

You need to construct the Role object first, you can not add constructor parameters directly to the list:

RoleList.Add(new Role(1, "Software Engineer"));

Edit:

RoleList is an object of type List<Role>, in other words it's a List of objects of type Role. The Add method requires a single parameter, which should have the type Role (or derived). When calling RoleList.Add(1, "Software Engineer") you pass two parameters - of type int and string, while the method Add only accepts one: List<Role>.Add(Role item). In which case you should be getting compiler error CS1501 No overload for method 'Add' takes 2 arguments.

To construct a Role object you must use the new keyword and provide constructor parameters if required: new Role(1, "Software Engineer").

Sign up to request clarification or add additional context in comments.

2 Comments

Can you be little precise ?
I would do a google search on Tuple it's really straight forward what @Maku has posted as an Answer in my Opinion

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.