Providing Access to a Class Private Member Attributes [duplicate]












0















This question already has an answer here:




  • Properties vs Methods

    15 answers




I was wondering what is more efficient or the best practice for returning attributes of private members. For example:



class Foo
{
private List<int> fooList;
public Foo()
{
Random random = new Random();
fooList = new List<int>(random.Next(1, 100));
}
//
public int Count { get { return fooList.Count; } }
// or
public int Count() { return fooList.Count; }
}


Which is best if I do not want to give public access to my list?










share|improve this question













marked as duplicate by Stijn, Fabjan, Foo, DavidG 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();
}
);
});
});
Nov 21 '18 at 15:57


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:




    • Properties vs Methods

      15 answers




    I was wondering what is more efficient or the best practice for returning attributes of private members. For example:



    class Foo
    {
    private List<int> fooList;
    public Foo()
    {
    Random random = new Random();
    fooList = new List<int>(random.Next(1, 100));
    }
    //
    public int Count { get { return fooList.Count; } }
    // or
    public int Count() { return fooList.Count; }
    }


    Which is best if I do not want to give public access to my list?










    share|improve this question













    marked as duplicate by Stijn, Fabjan, Foo, DavidG 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();
    }
    );
    });
    });
    Nov 21 '18 at 15:57


    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:




      • Properties vs Methods

        15 answers




      I was wondering what is more efficient or the best practice for returning attributes of private members. For example:



      class Foo
      {
      private List<int> fooList;
      public Foo()
      {
      Random random = new Random();
      fooList = new List<int>(random.Next(1, 100));
      }
      //
      public int Count { get { return fooList.Count; } }
      // or
      public int Count() { return fooList.Count; }
      }


      Which is best if I do not want to give public access to my list?










      share|improve this question














      This question already has an answer here:




      • Properties vs Methods

        15 answers




      I was wondering what is more efficient or the best practice for returning attributes of private members. For example:



      class Foo
      {
      private List<int> fooList;
      public Foo()
      {
      Random random = new Random();
      fooList = new List<int>(random.Next(1, 100));
      }
      //
      public int Count { get { return fooList.Count; } }
      // or
      public int Count() { return fooList.Count; }
      }


      Which is best if I do not want to give public access to my list?





      This question already has an answer here:




      • Properties vs Methods

        15 answers








      c#






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 15:46









      sfanjoy

      32247




      32247




      marked as duplicate by Stijn, Fabjan, Foo, DavidG 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();
      }
      );
      });
      });
      Nov 21 '18 at 15:57


      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 Stijn, Fabjan, Foo, DavidG 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();
      }
      );
      });
      });
      Nov 21 '18 at 15:57


      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.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          Based on your example, you should stay with a property. Because the fooList.Count is a property.






          share|improve this answer




























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Based on your example, you should stay with a property. Because the fooList.Count is a property.






            share|improve this answer


























              0














              Based on your example, you should stay with a property. Because the fooList.Count is a property.






              share|improve this answer
























                0












                0








                0






                Based on your example, you should stay with a property. Because the fooList.Count is a property.






                share|improve this answer












                Based on your example, you should stay with a property. Because the fooList.Count is a property.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 15:52









                Martin S.

                566




                566















                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python