且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

请解释一下这段代码。

更新时间:2023-11-09 22:51:34

public partial class _Default: System.Web.UI.Page



定义一个名为_ Default 的类,它继承自 System.Web.UI.Page partial 关键字指示编译器不会生成错误,因为在此范围内未找到类 _Default 的整个声明和定义。该类的其余部分应该在另一个类似的声明中提供(也使用部分关键字)。




Defines one class named _Default that inherits from System.Web.UI.Page. The partial keyword instructs the compiler to not generate errors for not finding at this scope the entire declarations and definitions for the class _Default. The rest of the class should be provided in another similar declaration (using the partial keyword also).

private static readonly string _connString = String.Empty;



声明一个私有(限定为类,不可见的字段)无法写入的字段(这是 readonly 的含义)。 static 关键字指定此字段将全局(共享)到该类的所有实例。

写入静态字段的代码不可重入,请注意这一点。

还有一件事,它被标记为 readonly 意味着它无法更改,因此使用值 String.Empty 初始化它是没有意义的,如果它稍后将像您的代码一样使用。




Declares one private (scoped to the class, invisible out of there) field which can not be written (that's the meaning of readonly). The static keyword specifies that this field will be global (shared by) to all instances of that class.
Code writing to static fields is not reentrant, take care with that.
One thing else, the fact that it is marked readonly implies that it can not be changed, so it makes no sense to initialize it with the value String.Empty if it will be used later like your code does.

SqlConnection con = new SqlConnection(_connString);



这将创建一个类的SqlConnection()。看看这个,因为 _connString 无法修改并且已经用空字符串初始化...大概你将无法与此代码建立连接。



也许您应该考虑从 _connString中删除 static readonly 声明。


This creates one instance of the class SqlConnection(). Take a look at this, as _connString can not be modified and has been initialized with an empty string... presumably you'll not be able to establish the connection with this code.

Perhaps you should consider removing static and readonly from your _connString declaration.


string.Empty字段是一个空字符串文字。



check 链接
The string.Empty field is an empty string literal.

check this link


在应用程序代码中,此字段最常用于分配以初始化字符串变量一个空字符串。要测试字符串的值是null还是String.Empty,请使用IsNull或Empty方法。
In application code, this field is most commonly used in assignments to initialize a string variable to an empty string. To test whether the value of a string is either null or String.Empty, use the IsNull Or Empty method.