c# - 报错不能在静态类中声明实例成员

标签 c# asp.net .net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;

我收到这个错误

cannot declare instance members in a static class

在编写静态类时。 知道如何解决它,如果可能的话保持类静态吗?谢谢

namespace XXX.Helpers
{
    public static class Utility
    {
        public static string GetConfiguration(string section, string key)
        {
            NameValueCollection mySection = (NameValueCollection)ConfigurationManager.GetSection(section);

            return mySection[key];
        }
    }
}

最佳答案

您不能在static 类中定义非静态成员。在静态类中声明实例成员是不合法的。

来自 Static Classes and Static Class Members (C# Programming Guide)

The following list provides the main features of a static class:

  • Contains only static members.

  • Cannot be instantiated.

  • Is sealed.

  • Cannot contain Instance Constructors.

我不得不说,顺便说一句,我在你的静态类中没有看到任何实例成员:)

关于c# - 报错不能在静态类中声明实例成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20989721/

相关文章:

c# - SignalR SQLTableDependency 数据库更改跟踪问题

jquery - 如何将jquery返回值添加到列表框中

c# - 使用 MVVM 在 WPF 中绑定(bind)命令

c# - 为什么 ListView 框中的项目都挤在一起?

c# - C# 默认位置中的配置文件?

C# LDAP 用户更改密码失败。 SSL连接是必须的吗?

c# - 模型状态错误过滤器

c# - 编写条件语句的替代方法

C# 字符串乘法错误

发布到 Azure 时,区域中的 ASP.NET Core 文件夹不会加载资源