Mastering Development

How bad is using pass by references in c#?

  public class HouseService 
        public void CreateHouse() 
            var house = new House() { Things = new List<Thing>() };
       //Using Pass By reference concept
        public void AddThings(House house) 
            //Adding Things here
            house.Things.Add(new Thing());

        //Returning List<Thing> from AddThings Method
        public List<Thing> AddThings(int houseId) 
            var things = new List<Thing>();
            things.Add(new Thing());
            return things;


In above code for adding things, we are sending House object and in AddThings() method we are adding things to same house object.

Here we are using "Pass by reference".

And we are having another AddThings method which we will send houseId and it will return list of things.

So which is better

  1. Using Pass By reference concept

  2. Returning List from AddThings Method.

I think Returning List is better because from method definition we can understand what it does..

Leave a Reply

Your email address will not be published. Required fields are marked *