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

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현