Inside a method, there can be only one object of any name. We avoided reusing the same variable names using the block level however, in an earlier example, however, an object with the same name outside the block area will show why this does not work. See This Example Demonstrating this Name Conflict:
public static void DoWork() { for (int i = 0; i < 10; i++) { Console.WriteLine(i); } int i = 777; // Compiler error here Console.WriteLine(i); }
Above from https://www.microsoft.com/net/tutorials/csharp/getting-started/scope-accessibility , I want to ask why this will happen? Why is C # created this way since C ++ and Java have nothing like this (I tested, there are no restrictions in Java and C ++)
,
, / , , .
, , , , .
, , 100% .
public static void DoWork() { for (int i = 0; i < 10; i++) { Console.WriteLine(i); } { // adding this block will remove the compiler error, // and yes you can use the same variable name in the same // method but you need to help the compiler that each // variable usuage is under its own block. int i = 777; // Not compiler error anymore Console.WriteLine(i); } }
, , ,
Source: https://habr.com/ru/post/1654961/More articles:How to fix the error: could not find com.google.gms: google-services: 3.0.0.? - androidSwift 3: Why is the _ character added before the sender in the action parameters? - iosHow to find out fragment identifier for fragment (s) provided by tab activity template - androidКак создать временную таблицу, указав группы файлов? - sql-server-2016Creating a query with environment variables in Paw - paw-appServer Key Management in Firebase Cloud Messaging - firebaseВложенные свойства объекта с динамическим именем - javascriptHow to access Powerpoint object events (Mac version) using Apple script? - powerpoint-vbaUITextField Chinese character moves down when editing in iOS 10 - ios'init' is not available: use 'withMemoryRebound (to: capacity: _)' - swiftAll Articles