Alb*_*rex 2 c# partial-classes
我有一个部分类的两部分:
public partial class Class1 : AnotherClass
{
int id;
}
public partial class Class1
{
public void func()
{
//here i need to access the id variable defined in the other part
id = 1; //this instruction raise an error "The name 'id' does not exists in the current context"
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何访问该变量?
您可以访问该字段,但您必须在某个方法/构造函数中访问它,您无法在类级别直接访问它.
public partial class Class1
{
public void SomeMethod()
{
id = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您正在进行字段初始化,那么如果在分部类中定义重载构造函数然后分配如下值,则会更好:
public partial class Class1
{
public Class1(int id)
{
this.id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
795 次 |
| 最近记录: |