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;








0
















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










share|improve this question













marked as duplicate by Ian Mercer c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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.
























    0
















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










    share|improve this question













    marked as duplicate by Ian Mercer c#
    Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

    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.




















      0












      0








      0









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










      share|improve this question















      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#






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 5:52









      abcabc

      92




      92




      marked as duplicate by Ian Mercer c#
      Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

      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 c#
      Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

      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.
























          2 Answers
          2






          active

          oldest

          votes


















          0














          you can have something like this



          public ReadOnlyCollection<string> MyValue get; private set;





          share|improve this answer






























            0














            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.






            share|improve this answer





























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              you can have something like this



              public ReadOnlyCollection<string> MyValue get; private set;





              share|improve this answer



























                0














                you can have something like this



                public ReadOnlyCollection<string> MyValue get; private set;





                share|improve this answer

























                  0












                  0








                  0







                  you can have something like this



                  public ReadOnlyCollection<string> MyValue get; private set;





                  share|improve this answer













                  you can have something like this



                  public ReadOnlyCollection<string> MyValue get; private set;






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 22 at 5:58









                  AsifAsif

                  594




                  594























                      0














                      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.






                      share|improve this answer



























                        0














                        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.






                        share|improve this answer

























                          0












                          0








                          0







                          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.






                          share|improve this answer













                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 22 at 6:01









                          John WuJohn Wu

                          31.7k42754




                          31.7k42754













                              Popular posts from this blog

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                              Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript