English 中文(简体)
如何在C#中宣布地方不变?
原标题:How to declare a local constant in 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());   
}

仅从事班级工作(即只能适用于田地)。 此外,由于要求字面字面的<代码>const,而read only/code> field可以是静态的,也可以是偶然的。

至2018-10-02年,在C#中可能只有一处当地读物,但有一个开放的proposal,该特征的siondiscussion

article 提供了有用的摘要。

有一种需要再收获的工作。 你可以只读当地人,但你至少可以发现变异者,以不同方式加以区别。

利用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.

MSDN

自2006年以来 C#可以执行“最正确的门槛”(如C++),我认为它非常有用。 由于职能范围很广,很容易失去监督。





相关问题
Anyone feel like passing it forward?

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

NSArray s, Primitive types and Boxing Oh My!

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

C# Marshal / Pinvoke CBitmap?

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

How to Use Ghostscript DLL to convert PDF to PDF/A

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

Linqy no matchy

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

热门标签