如何在C#中宣布地方不变?
同 Java一样,你可以做以下工作:
public void f(){
final int n = getNum(); // n declared constant
}
C#如何这样做? 我用<条码>只对条码>和<条码>查询,但似乎没有工作。
任何帮助都将受到高度赞赏。
感谢。
如何在C#中宣布地方不变?
同 Java一样,你可以做以下工作:
public void f(){
final int n = getNum(); // n declared constant
}
C#如何这样做? 我用<条码>只对条码>和<条码>查询,但似乎没有工作。
任何帮助都将受到高度赞赏。
感谢。
在C#中,你不能产生一种从某种方法中检索的不变。
Edit: dead link
http://msdn.microsoft.com/en-us/library/e6w8fe1b(VS.71).aspx
This doc should help: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/const
A constant expression is an expression that can be fully evaluated at compile time.
Declare your local variable as an iteration variable. Iteration variables are readonly (You didn t ask for a pretty solution).
public void f()
{
foreach (int n in new int[] { getNum() }) // n declared constant
{
n = 3; // won t compile: "error CS1656: Cannot assign to n because it is a foreach iteration variable "
}
}
我不肯定为什么<代码>只读和const
为你工作,因为这些都是你需要的关键词。 如果您有字面(除字面外)和<条码>,则请使用<条码>const。 否则:
public void f()
{
const int answer = 42;
}
private readonly int[] array = new int[] { 1, 2, 3, 4 };
private readonly DateTime date = DateTime.Now;
public void g()
{
Console.WriteLine(date.ToString());
}
read only/code> field可以是静态的,也可以是偶然的。
有一种需要再收获的工作。 你可以只读当地人,但你至少可以发现变异者,以不同方式加以区别。
利用Fons和Kolas项目Resharper Mutable Local Variable Identifier。
对我来说,我有当地人有色的灰色,然后,我选择了一种大胆的白色,用于变数(这是一个黑暗的主题)。 这意味着,任何一度以上的变数都显示,与常规变量相比,亮度有所提高。 那么,你可以做些什么来试图避免出现变数,或者如果这种方法确实需要变数,则至少会强调。
如你所介绍,你需要将变量宣布为<编码>static,因为你用一种方法重新启用。 如果您的初始价值与42一样,你可以使用<条码>const。 对于班级、结构和阵列,read only
应当工作。
The const keyword is used to modify a declaration of a field or local variable.
自2006年以来 C#可以执行“最正确的门槛”(如C++),我认为它非常有用。 由于职能范围很广,很容易失去监督。
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...