关于未分配的变量 [英] About unassigned variables

查看:141
本文介绍了关于未分配的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是好奇,我并不想解决什么问题。



为什么只有局部变量应分配?



在下面的例子:

 类节目
{
静态int类型的;
静态INT B {搞定;组; }
静态无效的主要(字串[] args)
{
INT℃;
的System.Console.WriteLine(一);
的System.Console.WriteLine(B);
的System.Console.WriteLine(C);
}
}



为什么 A b 给我只是一个警告和 C 给我一个错误?



Addionally,为什么我不能只是使用的值类型,写下面的代码?



的默认值

 布尔MyCondition = TRUE; 
INT℃;
如果(MyCondition)
C = 10;



是否有任何与内存管理?


解决方案

由于其他变量与初始化自己的 默认 值。



乔恩斯基特已经找到的一些有趣的话在这个问题上的:




有关局部变量,编译器的流量是一个好主意 -​​ 它可以
看到的变量和变量的写的读,并证明
(在大多数情况下),第一个写操作将第一
读取之前发生。



这是不是与实例变量的情况。考虑一个简单的
属性 - 你怎么知道,如果有人将它设置他们得到它之前?
这使得它基本上是不可行的执行懂事的规则 - 所以
要么你必须确保所有的字段都在
构造函数中设置,或让他们有默认值。 C#的团队选择了
后一种策略。




这里的相关的C#语言规范:




5.3明确赋值



在函数成员可执行代码中的给定位置,一个
变量被认为是明确赋值如果编译器可通过特定的静态流程分析证明,
(§ 5.3.3),该变量
被自动初始化或已成为至少一个
赋值的目标。



5.3.1开始赋值的变量



以下类别的变量列为最初$分配b $ A:




  • 静态变量。


  • 类实例的实例变量。


  • <最初分配的结构变量的p>实例变量。


  • 数组元素。


  • 值参数。


  • 参考参数。


  • 在catch子句或一个foreach声明的变量声明。




5.3.2最初未分配的变量



以下类别的变量划分为初始
未分配的:




  • 实例初始未赋值结构变量的变量


  • 输出参数,包括结构实例
    构造的这个变量。


  • 局部变量,除了那些在catch子句或
    foreach语句声明。




Just curious, I'm not trying to solve any problem.

Why only local variables should be assigned?

In the following example:

class Program
{
    static int a;
    static int b { get; set; }
    static void Main(string[] args)
    {
        int c;
        System.Console.WriteLine(a);
        System.Console.WriteLine(b);
        System.Console.WriteLine(c);
    }
}

Why a and b gives me just a warning and c gives me an error?

Addionally, why I can't just use the default value of Value Type and write the following code?

        bool MyCondition = true;
        int c;
        if (MyCondition)
            c = 10;

Does it have anything to do with memory management?

解决方案

Because other variables are initialized with their default value.

Jon Skeet has already found some interesting words on this issue:

For local variables, the compiler has a good idea of the flow - it can see a "read" of the variable and a "write" of the variable, and prove (in most cases) that the first write will happen before the first read.

This isn't the case with instance variables. Consider a simple property - how do you know if someone will set it before they get it? That makes it basically infeasible to enforce sensible rules - so either you'd have to ensure that all fields were set in the constructor, or allow them to have default values. The C# team chose the latter strategy.

and here's the related C# language specification:

5.3 Definite assignment

At a given location in the executable code of a function member, a variable is said to be definitely assigned if the compiler can prove, by a particular static flow analysis (§5.3.3), that the variable has been automatically initialized or has been the target of at least one assignment.

5.3.1 Initially assigned variables

The following categories of variables are classified as initially assigned:

  • Static variables.

  • Instance variables of class instances.

  • Instance variables of initially assigned struct variables.

  • Array elements.

  • Value parameters.

  • Reference parameters.

  • Variables declared in a catch clause or a foreach statement.

5.3.2 Initially unassigned variables

The following categories of variables are classified as initially unassigned:

  • Instance variables of initially unassigned struct variables.

  • Output parameters, including the this variable of struct instance constructors.

  • Local variables, except those declared in a catch clause or a foreach statement.

这篇关于关于未分配的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆