我有一个叫做雇员的类,它有一个叫做保险的字段,它是这样的保险类型
public class Employee
{
public string Name;
public Insurance Insurance;
}
我还有一门叫保险的课
public class Insurance
{
public int PolicyId;
public String PolicyName;
}
现在在主程序中我想做类似的事情
var myEmployee = new Employee();
myEmployee.Name = "Jhon";
myEmployee.Insurance.PolicyId = 123 ;
myEmployee.Insurance.PolicyName = "Life Time" ;
C# 正在抱怨,我知道如何通过创建 Insurance 类的实例来解决它。
我的问题是我能否以我想在主程序中使用 like 的方式分配字段的值
**
myEmployee.Insurance.PolicyId = 123 ;
myEmployee.Insurance.PolicyName = "Life Time" ;
** 我试过
public class Employee
{
public Employee()
{
Insurance Insurance = new Insurance();
}
public String Name;
public Insurance Insurance;
public class Insurance
{
public int PolicyId;
public String PolicyName;
}
}
在我尝试的主要方法中
class Program
{
static void Main(string[] args)
{
var joe = new Employee();
joe.Name = "Joe";
joe.Insurance.
}
我得到这个错误-
错误 2“ConsoleApplication1.Employee.Insurance”和“ConsoleApplication1.Employee.Insurance”之间的歧义 c:\users\lenovo\documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 15 17 ConsoleApplication1