本文介绍了通过在其他地方定义的某个地方声明的只读变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想声明无法修改的变量(比如readonly)

但是我想在单个类中声明它们所以我总是可以参考
$ b每当代码使用它们时,$ b都是同一个类。但是我想在不同文件中的不同类的构造函数中给它们值




实现此功能的最佳方法是什么? />

约翰·达尔伯格

I would like to declare variables that can''t be modified (like readonly)
however I want to declare them in a single class so I can always refer to
the same class whenever code uses them. However I want to give them values
in constructors in different classes in different files.

What''s the best way to accomplish this functionality?

John Dalberg

推荐答案





我想做这样的事情:


class static MyConstants

{

public readonly Name1;

public readonly Name2;

}

class A

{

A()

{

MyConstants.Name1 =" some name";

}

}

B级

{

B()

{

MyConstants.Name2 =" Some name";

}

}


想法是巩固所有非一个类中的可修改变量但是

在其他类中给出初始值。


John Dalberg

----- -------------------------------------------------- -



I want to do something like this:

class static MyConstants
{
public readonly Name1;
public readonly Name2;
}
class A
{
A()
{
MyConstants.Name1 = "Some name";
}
}
class B
{
B()
{
MyConstants.Name2 = "Some name";
}
}

The idea is to consolidate all non modifiable variable in one class but
give them an initial values in other classes.

John Dalberg
---------------------------------------------------------





这对我来说听起来不错。除了只读变量之外,变量是否包含什么?b $ b?它听起来并不像。


常量(等等)最好用相关的类表示 -

所以我们有int.MaxValue和double.MaxValue,不是Constants.MaxInt32和

Constants.MaxDouble等


-

Jon Skeet - < sk ***@pobox.com>
博客:

如果回复小组,请不要给我发邮件

That sounds like a bad idea to me. Do the variables have anything in
common other than being read-only? It doesn''t sound like it.

Constants (and the like) are best expressed in their related classes -
so we have int.MaxValue and double.MaxValue, not Constants.MaxInt32 and
Constants.MaxDouble etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于通过在其他地方定义的某个地方声明的只读变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 18:38
查看更多