CHAPTER 12
Variable Scoping and Definite Assignment
In C#, local variables must be given names that allow all variables to be uniquely identified throughout the method. Consider the following:
using System;class MyObject{ public MyObject(int x) { x = x; } int x;}
Since the compiler looks up parameters before it looks up member variables, the constructor in this example does not do anything useful; it copies the value of parameter x to parameter x.1 You can fix this by adding this. to the front of the name that you want to refer to the member variable.2
using System;class MyObject{ public MyObject(int x) { this.x ...
Get A Programmer's Guide to C# 5.0, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.