''Employee'是一种类型,在给定的上下文中无效

问题描述 投票:0回答:1

[当我尝试在Visual Studio中构建代码时,它显示错误为

  1. 'Employee'是一种类型,在给定的上下文中无效。
  2. 严重级别描述项目文件行错误CS0246找不到类型或名称空间名称“ C1”(您是缺少using指令或程序集引用?)SecondC C:\ Users \ ypoint \ source \ repos \ SecondC \ SecondC \ Program.cs 36

我是C#的新手,请帮助我解决这个问题。

using System;

public struct Emplyoee
{
    private int _Id;
    private string _name;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public int ID
    {
        get { return this._Id; }
        set { this._Id = value; }
    }

    public Emplyoee(int Id, string Name)
    {
        this._Id = Id;
        this._name = Name;
    }

    public void PrintDetails()
    {
        Console.WriteLine("Id={0} && Name={1}", this._Id, this._name);
    }
}

public class Program
{ 
    public static void Main()
    {
        Emplyoee C1 = new Emplyoee(101, "Sudharshan");
        C1.PrintDetails();

    }
}
.net visual-studio-2017 c#-3.0
1个回答
0
投票

您的类应在名称空间中定义,例如:

using System;

namespace Organization.Solution.Project // quite a regular way to define a namespace 
{
    public struct Emplyoee
    {
        // ...
    }

    public class Program
    {
        // ...
    }
}

现在Program类知道Emplyee结构,因为它们位于相同的名称空间中。不这样做意味着您的班级在global namespace中。

为了使代码文件更短,您可能希望拥有2个文件:

  • 带有Program类的Program.cs,包含在namespace Organization.Solution.Project

  • 带有Emplyoee结构的Emplyoee.cs,包含在namespace Organization.Solution.Project

© www.soinside.com 2019 - 2024. All rights reserved.