A question was asked about “the difference between Variable and Property”. In response to Mr. Patel’s answer. I pose a follow up question. “ Why am I allowed to make a change to the variable’s value, without invoking the property, when the Varible is still declared as Private?. The modified problem is listed below.
using System;
namespace Advanced_
{
class TestClass
{
private string x = “Test Test”;
void TestFun()
{
Console.WriteLine(x);
}
public string ChangeName
{
get { return x; }
set { x = value; }
}
// Application Execution begins Here!
static void Main(string[] args)
{
// Class instantiation / Objection creation begins
TestClass obj = new TestClass();
// First Method Call/ Invocation to Object 'TestFun' with original string value
obj.TestFun();
// Second Method Call/ Invocation to Object 'TestFun' with modified string value
// Note Property is not called.
obj.x = "Test Failure";
obj.TestFun();
//obj.ChangeName = "Property Testing ";
}