H
hee-chul
The below code can be compiled ant executed even though the Main
static method uses the 'private set' Property, CustomerId.
Is It Bug?
I'm using VS2008, .Net Framework 3.5.
Of course, If I moved the Main method into another file, then
compilation error is shown like this,
"The property of indexer "XTest.Program.CustomerId" cannot be used in
this context because the set accessor is inaccessible.
namespace XTest
{
public class Program
{
/// <summary>
/// Customer Id
/// </summary>
public int CustomerId { get; private set; }
/// <summary>
/// Default Constructor
/// </summary>
public Program()
{
CustomerId = 3;
}
/// <summary>
/// Dummy Method
/// </summary>
/// <returns>the magic #</returns>
public int GetMagicNumber()
{
return CustomerId * 10;
}
/// <summary>
/// External Main Method
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Program _x = new Program();
_x.CustomerId = 10;
Console.WriteLine("Cusomter : " + _x.CustomerId);
Console.WriteLine("MAGIC ## : " + _x.GetMagicNumber());
_x.CustomerId = 5; // Why does this expression work?
Console.WriteLine("Cusomter : " + _x.CustomerId);
Console.WriteLine("MAGIC ## : " + _x.GetMagicNumber());
}
}
}
static method uses the 'private set' Property, CustomerId.
Is It Bug?
I'm using VS2008, .Net Framework 3.5.
Of course, If I moved the Main method into another file, then
compilation error is shown like this,
"The property of indexer "XTest.Program.CustomerId" cannot be used in
this context because the set accessor is inaccessible.
namespace XTest
{
public class Program
{
/// <summary>
/// Customer Id
/// </summary>
public int CustomerId { get; private set; }
/// <summary>
/// Default Constructor
/// </summary>
public Program()
{
CustomerId = 3;
}
/// <summary>
/// Dummy Method
/// </summary>
/// <returns>the magic #</returns>
public int GetMagicNumber()
{
return CustomerId * 10;
}
/// <summary>
/// External Main Method
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Program _x = new Program();
_x.CustomerId = 10;
Console.WriteLine("Cusomter : " + _x.CustomerId);
Console.WriteLine("MAGIC ## : " + _x.GetMagicNumber());
_x.CustomerId = 5; // Why does this expression work?
Console.WriteLine("Cusomter : " + _x.CustomerId);
Console.WriteLine("MAGIC ## : " + _x.GetMagicNumber());
}
}
}