Best OOP Practice in C#: Passing the Object as Parameter VS Creating a New Instance of the Object

First of all, I would like to know if passing an object as parameter is much better than creating another object again in a class that will use it again and second what are the pros and cons of each one?

Here’s an example:

public class ClassWithTheObject
{

TheObject obj = new TheObject();

// Somewhere around the code
PassTheObjectHere anotherObj = new PassTheObjectHere(obj);

}

Or just go straight declaring the class TheObject inside the PassTheObjectHere class? The case would be that both ClassWithTheObject and PassTheObjectHere classes will use the TheObject class.

Last question, is passing the object too many times causes some bad effects on the program?

Please enlighten me.

2

Your question shows some misconceptions, I guess: if the alternative to

passing an object as parameter

is to create a new instance directly in the constructor PassTheObjectHere, then you are obviously not using any values/state of the previously created obj within PassTheObjectHere. If that is the case, it is pretty useless to pass obj as a parameter to PassTheObjectHere. In fact, in such a case I would typically check if TheObject has any member variables, which might make it a candidate for a static class (but don’t get me wrong, I am not telling the usage of a static class would improve the design here in any way).

On the other hand, if the constructor of PassTheObjectHere needs the values / state of the previously created obj to work properly, it would be plain wrong to have a constructor like this.

 PassTheObjectHere()
 {
      var obj = new TheObject();

      // ... do something which expects having obj
      // ... some values provided by the caller
 }

(I hope this is obvious).

can passing the object too many times cause some bad effects on the program?

This is not a question of “too many times”. One can pass objects around 1000 times correctly, which is fine, and one time wrong, which is bad. For example, if one passes obj to the constructor, and the constructor changes the state of obj in a way the caller does not expect (called a “side effect”)

  PassTheObjectHere(TheObject obj)
  {
       // ... use methods/properties/values of obj 
       // ... for this constructor

       // and finally
       obj.MakeValuesInvalid();
  }

and the caller does something like

public class ClassWithTheObject
{

   TheObject obj = new TheObject();
   obj.Initialize()
   PassTheObjectHere anotherObj = new PassTheObjectHere(obj);
   obj.MethodWhichExpectsObjToBeValid();
}

then the program now has a bug.

Fortunately, one can use one of the following alternatives to protect oneself from that:

  1. Avoid the side effect by not changing “obj” within the constructor
  2. If one needs to change “obj”, they can make a copy beforehand within the constructor.
  3. Avoid adding methods to TheObject which allow the change of internal state of obj (which is called “immutability”)

Number 3 leads to the same code as 1, but with additional protection against introducing unintentional side effects at a later point in time, when PassTheObjectHere might be changed.

0

It’s a bit more complicated than that.

Firstly, understand the tradeoffs. If you are going to pass a copy of the object (essentially “pass by value” semantics) rather than a reference to the original object, you are going to take a performance hit. Whether that hit is justified or not depends on your software’s functional requirements.

Secondly, it might make more sense semantically to write methods that accept the object by reference but return a new object, rather than modifying the passed object in-place or making a copy of it before passing it to the method.

var modified object = ReturnANewObjectFrom(theOriginalObject);

When you create and return new objects in this fashion, you are taking advantage of immutability. Wherever immutability makes sense, this makes sense to do. Programs which utilize immutable objects and data structures are easier to reason about, especially when writing concurrent applications (applications that do more than one thing at a time, usually in separate threads or on separate cores).

3

I think it really depends on the context you are working with. In your example, you are sending the TheObject instance to the PassTheObjectHere constructor, this way of working tells me that, inside your PassTheObjectHere instance, you are working with some previous initializated data. So if the ClassWithTheObject is going to share the conversational state of the TheObject instance, you are doing right. Thats what i think

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Best OOP Practice in C#: Passing the Object as Parameter VS Creating a New Instance of the Object

First of all, I would like to know if passing an object as parameter is much better than creating another object again in a class that will use it again and second what are the pros and cons of each one?

Here’s an example:

public class ClassWithTheObject
{

TheObject obj = new TheObject();

// Somewhere around the code
PassTheObjectHere anotherObj = new PassTheObjectHere(obj);

}

Or just go straight declaring the class TheObject inside the PassTheObjectHere class? The case would be that both ClassWithTheObject and PassTheObjectHere classes will use the TheObject class.

Last question, is passing the object too many times causes some bad effects on the program?

Please enlighten me.

2

Your question shows some misconceptions, I guess: if the alternative to

passing an object as parameter

is to create a new instance directly in the constructor PassTheObjectHere, then you are obviously not using any values/state of the previously created obj within PassTheObjectHere. If that is the case, it is pretty useless to pass obj as a parameter to PassTheObjectHere. In fact, in such a case I would typically check if TheObject has any member variables, which might make it a candidate for a static class (but don’t get me wrong, I am not telling the usage of a static class would improve the design here in any way).

On the other hand, if the constructor of PassTheObjectHere needs the values / state of the previously created obj to work properly, it would be plain wrong to have a constructor like this.

 PassTheObjectHere()
 {
      var obj = new TheObject();

      // ... do something which expects having obj
      // ... some values provided by the caller
 }

(I hope this is obvious).

can passing the object too many times cause some bad effects on the program?

This is not a question of “too many times”. One can pass objects around 1000 times correctly, which is fine, and one time wrong, which is bad. For example, if one passes obj to the constructor, and the constructor changes the state of obj in a way the caller does not expect (called a “side effect”)

  PassTheObjectHere(TheObject obj)
  {
       // ... use methods/properties/values of obj 
       // ... for this constructor

       // and finally
       obj.MakeValuesInvalid();
  }

and the caller does something like

public class ClassWithTheObject
{

   TheObject obj = new TheObject();
   obj.Initialize()
   PassTheObjectHere anotherObj = new PassTheObjectHere(obj);
   obj.MethodWhichExpectsObjToBeValid();
}

then the program now has a bug.

Fortunately, one can use one of the following alternatives to protect oneself from that:

  1. Avoid the side effect by not changing “obj” within the constructor
  2. If one needs to change “obj”, they can make a copy beforehand within the constructor.
  3. Avoid adding methods to TheObject which allow the change of internal state of obj (which is called “immutability”)

Number 3 leads to the same code as 1, but with additional protection against introducing unintentional side effects at a later point in time, when PassTheObjectHere might be changed.

0

It’s a bit more complicated than that.

Firstly, understand the tradeoffs. If you are going to pass a copy of the object (essentially “pass by value” semantics) rather than a reference to the original object, you are going to take a performance hit. Whether that hit is justified or not depends on your software’s functional requirements.

Secondly, it might make more sense semantically to write methods that accept the object by reference but return a new object, rather than modifying the passed object in-place or making a copy of it before passing it to the method.

var modified object = ReturnANewObjectFrom(theOriginalObject);

When you create and return new objects in this fashion, you are taking advantage of immutability. Wherever immutability makes sense, this makes sense to do. Programs which utilize immutable objects and data structures are easier to reason about, especially when writing concurrent applications (applications that do more than one thing at a time, usually in separate threads or on separate cores).

3

I think it really depends on the context you are working with. In your example, you are sending the TheObject instance to the PassTheObjectHere constructor, this way of working tells me that, inside your PassTheObjectHere instance, you are working with some previous initializated data. So if the ClassWithTheObject is going to share the conversational state of the TheObject instance, you are doing right. Thats what i think

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật