請記住,這是我在這裏的第一個問題,我也是C#的新手。如何在Visual Studio中構建此代碼?
我將代碼從this YouTube video複製到Visual Studio,但它不會生成。我無法將項目添加到empList。
更新
我真的很感謝你的幫助,但它仍然沒有,儘管你的建議建設(例如我確實有使用System.Collections.Generic
命名空間)。我甚至完全複製了Rion的代碼,但仍然無法構建。這些行有錯誤:
)預計「,」;預計
和
無效表達術語「」
List<Employee> empList = new List<Employee>();
empList.Add(new Employee() {ID = 101, Name = "Mary", Salary = 5000, Experience = 5});
empList.Add(new Employee() {ID = 102, Name = "Maria", Salary = 6000, Experience = 2});
empList.Add(new Employee() {ID = 106, Name = "Mike", Salary = 4000, Experience = 4});
empList.Add(new Employee() {ID = 104, Name = "John", Salary = 5000, Experience = 7});
Employee.PromoteEmployee(empList);
它不完全一樣在視頻中的代碼,因爲我還是改了一點,但它不會建立。
class Program
{
public static void Main(string[] args)
{
List<Employee> empList = new List<Employee>();
empList.Add(new Employee() {ID = 101, Name = "Mary", Salary = 5000, Experience = 5});
empList.Add(new Employee() {ID = 102, Name = "Maria", Salary = 6000, Experience = 2});
empList.Add(new Employee() {ID = 106, Name = "Mike", Salary = 4000, Experience = 4});
empList.Add(new Employee() {ID = 104, Name = "John", Salary = 5000, Experience = 7});
Employee.PromoteEmployee(empList);
}
}
class Employee
{
public int ID;
public string Name;
public int Salary;
public int Experience;
public static void PromoteEmployee(List<Employee> employeeList)
{
foreach (Employee employee in employeeList)
{
if(employee.Experience >=5)
{
Console.WriteLine(employee.Name + " promoted");
Console.ReadLine();
}
}
}
}
如果它「不會構建」,那麼想必你會得到一個編譯器錯誤。您必須努力盡可能多地發佈有關您問題的信息。那麼這個錯誤信息是什麼? –
我在這段代碼中看不到任何錯誤。正如已經問到的,請解釋您的問題和收到的任何錯誤消息。 – Steve
這些類在同一個命名空間中嗎? – Master117