c# - How to sum multiple objects together -
lets have 3 objects
object o1, o2, o3 these objects have values of
o1 = 1 o2 = 2 o3 = 3 for simplicity sake
now add these objects sum of 6 + operator cannot used objects. ways can these objects summed without extensively converting them?
the reason using objects rather int etc, due using datatable gets populated sql query. take value datatable , assign o1 example.
you can overload plus operator if make own object type. suspect there other ways this, example:
i take value datatable , assign o1 example.
you assign int @ time. if want use object, because need create more behavior in future, on object type would:q
// overload operator + public static yourobject operator +(yourobject a, yourobject b) { return new yourobject(a.thevaluepropertyint + b.thevaluepropertyint); } but not going able sum types of c#'s object because not overload + operator.
fundamentally trying use object specific behavior (integer math) without telling compiler want int behaviors.
ultimately, ask if int, or other type. if int, cast extract.
Comments
Post a Comment