小编sku*_*any的帖子

C# const protected 与内部

为什么“内部常量”在子类中被覆盖,而“受保护常量”却不能?

示例代码:

    class A
    {
        internal const string iStr = "baseI";
        protected const string pStr = "baseP";

        void foo()
        {
            string s = B.iStr; //childI
            string t = B.pStr; //baseP
        }
    }

    class B : A
    {
        internal new const string iStr = "childI";
        protected new const string pStr = "childP";
    }
Run Code Online (Sandbox Code Playgroud)

预期 B.pStr 返回“childP”。

.net c# constants protected internals

2
推荐指数
1
解决办法
132
查看次数

标签 统计

.net ×1

c# ×1

constants ×1

internals ×1

protected ×1