'private set' in auto-implemented Properties would not work


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());

}
}
}
 
Ad

Advertisements

F

Family Tree Mike

hee-chul said:
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());

}
}
}
.

That is the way that a private setter should work, auto-implemented or
otherwise. The property is part of the program class as is the main method,
so main has access to the set method.

When you move the property to another class, then of course, the set method
is not accessable.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top