Restrict access to property from outside the class [duplicate] The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceIEnumerable vs IReadonlyCollection vs ReadonlyCollection for exposing a list memberHidden Features of C#?How do you give a C# Auto-Property a default value?Deep cloning objectsShould 'using' directives be inside or outside the namespace?LINQ's Distinct() on a particular propertyPassing arguments to C# generic new() of templated typeSetting a property by reflection with a string valuePublic Fields versus Automatic PropertiesGet properties and values from unknown objectWhy not inherit from List<T>?
Is there a way to generate uniformly distributed points on a sphere from a fixed amount of random real numbers per point?
Why are PDP-7-style microprogrammed instructions out of vogue?
What can I do if neighbor is blocking my solar panels intentionally?
How to determine omitted units in a publication
For what reasons would an animal species NOT cross a *horizontal* land bridge?
Was credit for the black hole image misappropriated?
How did passengers keep warm on sail ships?
What to do when moving next to a bird sanctuary with a loosely-domesticated cat?
Can a flute soloist sit?
Why can't wing-mounted spoilers be used to steepen approaches?
Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?
Example of compact Riemannian manifold with only one geodesic.
Didn't get enough time to take a Coding Test - what to do now?
"... to apply for a visa" or "... and applied for a visa"?
Do working physicists consider Newtonian mechanics to be "falsified"?
Using dividends to reduce short term capital gains?
How do you keep chess fun when your opponent constantly beats you?
Identify 80s or 90s comics with ripped creatures (not dwarves)
Mortgage adviser recommends a longer term than necessary combined with overpayments
Are spiders unable to hurt humans, especially very small spiders?
How to make Illustrator type tool selection automatically adapt with text length
How to handle characters who are more educated than the author?
Huge performance difference of the command find with and without using %M option to show permissions
How to support a colleague who finds meetings extremely tiring?
Restrict access to property from outside the class [duplicate]
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceIEnumerable vs IReadonlyCollection vs ReadonlyCollection for exposing a list memberHidden Features of C#?How do you give a C# Auto-Property a default value?Deep cloning objectsShould 'using' directives be inside or outside the namespace?LINQ's Distinct() on a particular propertyPassing arguments to C# generic new() of templated typeSetting a property by reflection with a string valuePublic Fields versus Automatic PropertiesGet properties and values from unknown objectWhy not inherit from List<T>?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
IEnumerable vs IReadonlyCollection vs ReadonlyCollection for exposing a list member
3 answers
Let's have this class:
ExampleClass
public List<string> MyValue get; set;
The question is how to restrict outside classes to modify of that property, means add object to collection, make new().
c#
marked as duplicate by Ian Mercer
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 5:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
IEnumerable vs IReadonlyCollection vs ReadonlyCollection for exposing a list member
3 answers
Let's have this class:
ExampleClass
public List<string> MyValue get; set;
The question is how to restrict outside classes to modify of that property, means add object to collection, make new().
c#
marked as duplicate by Ian Mercer
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 5:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
IEnumerable vs IReadonlyCollection vs ReadonlyCollection for exposing a list member
3 answers
Let's have this class:
ExampleClass
public List<string> MyValue get; set;
The question is how to restrict outside classes to modify of that property, means add object to collection, make new().
c#
This question already has an answer here:
IEnumerable vs IReadonlyCollection vs ReadonlyCollection for exposing a list member
3 answers
Let's have this class:
ExampleClass
public List<string> MyValue get; set;
The question is how to restrict outside classes to modify of that property, means add object to collection, make new().
This question already has an answer here:
IEnumerable vs IReadonlyCollection vs ReadonlyCollection for exposing a list member
3 answers
c#
c#
asked Mar 22 at 5:52
abcabc
92
92
marked as duplicate by Ian Mercer
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 5:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Ian Mercer
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 5:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
you can have something like this
public ReadOnlyCollection<string> MyValue get; private set;
add a comment |
You could expose it as IEnumerable<string>
instead of as a list. This interface will not allow adds. You can still store it as a list internally, as a private field, so that the class itself can add or remove if needed.
For example:
class ExampleClass
private List<string> _myValue = new List<string>();
public IEnumerable<string> MyValue
get
foreach (var s in _myValue) yield return s;
If the caller would like to work with its own list, it can of course do this:
var list = exampleClass.MyValue.ToList();
At which point the caller owns it and it is clear that anything it chooses to add has nothing to do with the original list.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
you can have something like this
public ReadOnlyCollection<string> MyValue get; private set;
add a comment |
you can have something like this
public ReadOnlyCollection<string> MyValue get; private set;
add a comment |
you can have something like this
public ReadOnlyCollection<string> MyValue get; private set;
you can have something like this
public ReadOnlyCollection<string> MyValue get; private set;
answered Mar 22 at 5:58
AsifAsif
594
594
add a comment |
add a comment |
You could expose it as IEnumerable<string>
instead of as a list. This interface will not allow adds. You can still store it as a list internally, as a private field, so that the class itself can add or remove if needed.
For example:
class ExampleClass
private List<string> _myValue = new List<string>();
public IEnumerable<string> MyValue
get
foreach (var s in _myValue) yield return s;
If the caller would like to work with its own list, it can of course do this:
var list = exampleClass.MyValue.ToList();
At which point the caller owns it and it is clear that anything it chooses to add has nothing to do with the original list.
add a comment |
You could expose it as IEnumerable<string>
instead of as a list. This interface will not allow adds. You can still store it as a list internally, as a private field, so that the class itself can add or remove if needed.
For example:
class ExampleClass
private List<string> _myValue = new List<string>();
public IEnumerable<string> MyValue
get
foreach (var s in _myValue) yield return s;
If the caller would like to work with its own list, it can of course do this:
var list = exampleClass.MyValue.ToList();
At which point the caller owns it and it is clear that anything it chooses to add has nothing to do with the original list.
add a comment |
You could expose it as IEnumerable<string>
instead of as a list. This interface will not allow adds. You can still store it as a list internally, as a private field, so that the class itself can add or remove if needed.
For example:
class ExampleClass
private List<string> _myValue = new List<string>();
public IEnumerable<string> MyValue
get
foreach (var s in _myValue) yield return s;
If the caller would like to work with its own list, it can of course do this:
var list = exampleClass.MyValue.ToList();
At which point the caller owns it and it is clear that anything it chooses to add has nothing to do with the original list.
You could expose it as IEnumerable<string>
instead of as a list. This interface will not allow adds. You can still store it as a list internally, as a private field, so that the class itself can add or remove if needed.
For example:
class ExampleClass
private List<string> _myValue = new List<string>();
public IEnumerable<string> MyValue
get
foreach (var s in _myValue) yield return s;
If the caller would like to work with its own list, it can of course do this:
var list = exampleClass.MyValue.ToList();
At which point the caller owns it and it is clear that anything it chooses to add has nothing to do with the original list.
answered Mar 22 at 6:01
John WuJohn Wu
31.7k42754
31.7k42754
add a comment |
add a comment |