如何访问班级字段

时间:2019-01-04 17:55:44

标签: c#

我正在学习c#语言,并且仍然是了解变量范围的基础知识。我不确定如何访问C2类中的C1类中的文件

我在声明int i时尝试使用public关键字

using System;

namespace MyClass
{
    class C1
    {
        int i = 5;
    }
    class C2
    {
        int j = i;
        static void Main()
        {
            Console.WriteLine($"Value is {i}");
        }
    }
}

我希望输出“值是5”,但出现以下错误

test.cs(11,10): error CS0103: The name 'i' does not exist in the current context
test.cs(14,33): error CS0103: The name 'i' does not exist in the current context
test.cs(7,6): warning CS0414: The field 'C1.i' is assigned but its value is never used

1 个答案:

答案 0 :(得分:-1)

C1 c1instance =新的C1(); int j = C1.i;

哦,改变整数i = 5;到公开诠释,我= 5;

这并不完美,但这应该可以帮助您入门