zoukankan      html  css  js  c++  java
  • What does “Use of unassigned local variable” mean?

    What does “Use of unassigned local variable” mean?

    The compiler isn't smart enough to know that at least one of your if blocks will be executed. Therefore, it doesn't see that variables like annualRate will be assigned no matter what. Here's how you can make the compiler understand:

    if (creditPlan == "0")
    {
        // ...
    }
    else if (creditPlan == "1")
    {
        // ...
    }
    else if (creditPlan == "2")
    {
        // ...
    }
    else
    {
        // ...
    }

    The compiler knows that with an if/else block, one of the blocks is guaranteed to be executed, and therefore if you're assigning the variable in all of the blocks, it won't give the compiler error.

    By the way, you can also use a switch statement instead of ifs to maybe make your code cleaner.

    Are C# uninitialized variables dangerous?

    I am under the impression that there are not truly "unassigned" values allowed by the runtime. In particular that a reference type that is not initialized will always have a null value, never the value left over from a previous invocation of the method or random value. Is this correct?

    I note that no one has actually answered your question yet.

    The answer to the question you actually asked is "sorta".

    As others have noted, some variables (array elements, fields, and so on) are classified as being automatically "initially assigned" to their default value. (Which is null for reference types, zero for numeric types, false for bools, and the natural recursion for user-defined structs).

    Some variables are not classified as initially assigned; local variables in particular are not initially assigned. They must be classified by the compiler as "definitely assigned" at all points where their values are used.

    Your question then is actually "is a local variable that is classified as not definitely assigned actually initially assigned the same way that a field would be?" And the answer to that question is yes, in practice, the runtime initially assigns all locals.

    This has several nice properties. First, you can observe them in the debugger to be in their default state before their first assignment. Second, there is no chance that the garbage collector will be tricked into dereferencing a bad pointer just because there was garbage left on the stack that is now being treated as a managed reference. And so on.

    The runtime is permitted to leave the initial state of locals as whatever garbage happened to be there if it can do so safely. But as an implementation detail, it does not ever choose to do so. It zeros out the memory for a local variable aggressively.

    The reason then for the rule that locals must be definitely assigned before they are used is not to prevent you from observing the garbage uninitialized state of the local. That is already unobservable because the CLR aggressively clears locals to their default values, the same as it does for fields and array elements. The reason this is illegal in C# is because using an unassigned local has high likelihood of being a bug. We simply make it illegal, and then the compiler prevents you from ever having such a bug.

  • 相关阅读:
    Java8新特性之Collectors
    java日期的运用(DateUtils工具类)
    正则表达式30分钟入门教程
    一篇非常好的Spring教程
    结合实际需求,在webapi内利用WebSocket建立单向的消息推送平台,让A页面和服务端建立WebSocket连接,让其他页面可以及时给A页面推送消息
    关于企业微信对接内部应用开发,access_token的管理机制和业务接口调用项目实战的八个要点
    企业微信使用DevTools但显示为空白,解决方法
    16.刚体碰撞事件监测与处理
    15.碰撞体
    14.刚体组件Rigidbody
  • 原文地址:https://www.cnblogs.com/chucklu/p/13157078.html
Copyright © 2011-2022 走看看