about property

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

I just wonder what this mean it is written in a book called "Visual C#
2005"
" The accessibilities that are permitted for accessors depends on the
accessibility of the property,
and it is forbiden to make an accessor more accessible than the property it
belongs to. This means that a private property cannot contain any
accessibility modifiers for its accessors, while public properties can call
all modifiers on their accessors"

Can you give an example.

//Tony
 
A public property with a private "set" accessor - only the class itself
can call "set":

private int foo;
public int Foo
{
get { return foo; }
private set { foo = value; }
}

You cannot, however, have a private property with a public accessor.

Marc
 
After serious thinking Tony Johansson wrote :
Hello!

I just wonder what this mean it is written in a book called "Visual C# 2005"
" The accessibilities that are permitted for accessors depends on the
accessibility of the property,
and it is forbiden to make an accessor more accessible than the property it
belongs to. This means that a private property cannot contain any
accessibility modifiers for its accessors, while public properties can call
all modifiers on their accessors"

Can you give an example.

//Tony

Roughly translated:

you can do:
public string MyProperty { get; private set; }

but you can't get the same through:
private string MyProperty { public get; set; }

You can only set a "more restrictive" access on either the "get" or the
"set", not a "less restrictive" one.

Hans Kesting
 

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

Back
Top