错误不能在静态类中声明实例成员 [英] Error cannot declare instance members in a static class

查看:240
本文介绍了错误不能在静态类中声明实例成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Collections.Specialized;
使用System.Configuration;

我收到此错误

 不能在静态类实例成员声明

写一个静态类时


不知道如何解决它保持类的静态如果可能的话?谢谢

 命名空间XXX.Helpers
{
    公共静态类工具
    {
        公共静态字符串GetConfiguration(弦乐部分,字符串键)
        {
            NameValueCollection中mySection =(NameValueCollection中)ConfigurationManager.GetSection(部分);            返回mySection [关键]
        }
    }
}


解决方案

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

静态类和静态类成员(C#编程指南)


  

下面的列表提供了一个静态类的主要特点:


  
  

      
  • 只包含静态成员。


  •   
  • 不能被实例化。


  •   
  • 是密封的。


  •   
  • 不能包含实例构造函数。


  •   

我不得不说,我没有看到你的静态类的实例成员的方式:)

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

I am receiving this error

cannot declare instance members in a static class

when writing a Static class. Any idea how to solve it keeping the class static if possible? Thanks

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

            return mySection[key];
        }
    }
}

解决方案

You can not define non-static members in a static class. It is not legal to declare an instance member in a static class.

From 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.

I have to say, I don't see any instance member in your static class by the way :)

这篇关于错误不能在静态类中声明实例成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆