I have a working sample code, and I can not explain why this is working like this.
Take this class for example.
public class TestClass
{
private int _testValue;
public void TestMethod()
{
var t = new TestClass() {_testValue = 42}; // Why is that working?
}
}
Why is this allowed? I would have expected having the behavior as this:
static void Main(string[] args)
{
new TestClass() { _testValue = 23 }; // Does not compile and I am happy with that.
// The code provided will print ‘Hello World’ to the console.
// Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app.
Console.WriteLine("Hello World!");
Console.ReadKey();
// Go to http://aka.ms/dotnet-get-started-console to continue learning how to build a console app!
}
Edit: Tank you for your comments, but I know what private keyword mean, the question is, why Can I have access to the private properties when I am using a parameter constructor when I am inside the same object.
Place a debugger, you will see that the TestClass this._testValue is not the same as the t._testValue
The normal behavior in my mind is the one that I have in the Main function.
