简体   繁体   English

是否需要实例化C#静态类?

[英]Does a C# static class need to be instantiated?

I am a new .NET developer. 我是新的.NET开发人员。 I would like to know whether a static class in C# needs to be instantiated? 我想知道C#中的static class是否需要实例化吗? I created a class for a database connection: 我为数据库连接创建了一个class

public static class ConnectionHelper
{
    public static SqlConnection GetConnection()
    {
        return new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    }
}

I am instantiating this class in my C# application and getting the following error: 我正在C#应用程序中实例化此类,并收到以下错误:

The name 'ConnectionHelper' does not exists in the current context 名称“ ConnectionHelper”在当前上下文中不存在

The related code: 相关代码:

using (var cn = ConnectionHelper.GetConnection())
{
    ...
}

Please advise. 请指教。

No, a static class does not need to be instantiated. 不, static class不需要实例化。 It cannot be instantiated, that's the point of the static keyword in class declaration. 不能被实例化,这就是类声明中static关键字的意义。

You are not instantiating it, either. 您也没有实例化它。 What you do is call a static method from a static class. 您要做的是从静态类中调用静态方法。 And that's fine. 很好。 But instantiating a class requires the new keyword. 但是实例化一个类需要new关键字。

You are probably missing a using -directive at the top of your file to make it compile. 您可能在文件顶部缺少using指令进行编译。

I think you're missing a namespace. 我认为您缺少名称空间。

Add the namespace of your ConnectionHelper to your "using"-block. 将ConnectionHelper的名称空间添加到“ using”块中。 For example: 例如:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConnectionHelperNamespace;

If your static class defined in separate project, then add refernce to it in Project Explorer: http://msdn.microsoft.com/en-us/library/wkze6zky(v=vs.80).aspx and after that add the namespace to "using"-block. 如果您的静态类在单独的项目中定义,则在项目资源管理器中为其添加引用: http : //msdn.microsoft.com/zh-cn/library/wkze6zky( v=vs.80) .aspx ,然后添加名称空间“使用”块。

You can try this by removing the satic in "public static class": 您可以通过删除“ public static class”中的satic来尝试此操作:

public class ConnectionHelper
{
    public static SqlConnection GetConnection()
    {
        return new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    }
}

It will work, but before being added some non-static attributes or methods, the instance can do nothing. 它可以工作,但是在添加一些非静态属性或方法之前,该实例无法执行任何操作。

Classes create stateful objects - on which we perform various operations. 类创建有状态的对象-我们对其执行各种操作。 For this we have to declare and instantiate objects. 为此,我们必须声明和实例化对象。

static classes / methods are meant to be stateless. static类/方法是无状态的。 Mostly we just want static methods to receive certain parameters , perform operation and return values / status . 通常,我们只希望static方法接收某些参数执行操作返回值/状态

They are not supposed to store any intermediate results for which we need class data members. 它们不应存储我们需要类数据成员的任何中间结果。

So, simply-put, static class is not instantiated! 因此,简单实例化的static class不会实例化!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM