Click here to Skip to main content
14,973,498 members
Articles / Web Development / ASP.NET
Posted 31 Aug 2011

Tagged as


5 bookmarked

To find the length of the Business Object at run time

Rate me:
Please Sign up or sign in to vote.
3.88/5 (8 votes)
31 Aug 2011CPOL
To find the length of the Business Object at run time
Business Object: A business object is an entity that relates to real world. In C# we create classes to define the real world entities. This class contains variables, and for each variable we have its properties and methods defined.
Collection of such properties forms a business object.

Now comes a scenario where we have a class that contains 40-50 properties or more, and each property has a different data type. Calculating the length of the business object that how many bytes will the object of this class will consume is the question.

Now consider a class with various data members:
public class UserBO 

    #region Private Variables
        private int _UserID;
        private int _CurrentID;
        private int _SalutationID;
        private int _NameID;
        private int _AuditID;
        private string _FirstName;
        private string _LastName;
        private string _Suffix;
        private string _MiddleInitial;

        private string _NickName;
	Nth private variable
#region Properties
	[DataMember(Name = "UserID", IsRequired = false, Order = 1)]
        public int UserID
            get { return (_UserID); }
            set { _ UserID = value; }

        [DataMember(Name = " CurrentID ", IsRequired = false, Order = 2)]
        public int CurrentID
            get { return (_CurrentID); }
            set { _ CurrentID = value; }

        [DataMember(Name = " SalutationID ", IsRequired = false, Order = 3)]
        public int SalutationID
            get { return (_SalutationID); }
            set { _ SalutationID = value; }

        [DataMember(Name = "AuditID", IsRequired = false, Order = 4)]
        public int AuditID
            get { return (_AuditID); }
            set { _AuditID = value; }

        [DataMember(Name = "NameID", IsRequired = false, Order = 5)]
        public int NameID
            get { return (_NameID); }
            set { _ NameID = value; }

        [DataMember(Name = " FirstName ", IsRequired = false, Order = 6)]
        public string FirstName
            get { return (_FirstName == null) ? string.Empty : _ FirstName; }
            set { _ FirstName = value; }


        [DataMember(Name = "LastName", IsRequired = false, Order = 7)]
        public string LastName
            get { return (_LastName == null) ? string.Empty : _ LastName; }
            set { _ LastName = value; }

        [DataMember(Name = "Suffix", IsRequired = false, Order = 8)]
        public string Suffix
            get { return (_Suffix == null) ? string.Empty : _ Suffix; }
            set { _ Suffix = value; }

        [DataMember(Name = "MiddleName", IsRequired = false, Order = 9)]
        public string MiddleName
 	get { return (_MiddleName == null) ? string.Empty : _ MiddleName;    }
            set { _ MiddleName = value; }

        [DataMember(Name = "NickName", IsRequired = false, Order = 10)]
        public string NickName
            get { return (_NickName == null) ? string.Empty : _ NickName; }
            set { _ NickName = value; }
	Nth proerties

Now UserBO contains plenty of properties, at run time we need to calculate the size, thet how many bytes will the object of UserBO will consume.
To calculate the size of the BO there is a simple way, the method in which I am creating the object of my class after getting the properties bind, I added this code

//pObjUserBO is the object of my class UserBO
BinaryFormatter bf = new BinaryFormatter();
 MemoryStream ms = new MemoryStream();
 bf.Serialize(ms, pObjUserBO);
 return ms.ToArray();

ms.ToArray is the size that my business object will consume at runtime.

Hope it helps you in finding the length of your business object at run time :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Anuja Pawar Indore
Program Manager Infobeans
India India
I have keen interest in learning new things, exploring more on a topic and being more versatile

Comments and Discussions

GeneralMy vote of 1 Pin
aSarafian5-Dec-12 21:45
MemberaSarafian5-Dec-12 21:45 
QuestionI disagree Pin
aSarafian5-Dec-12 21:43
MemberaSarafian5-Dec-12 21:43 
I'm sorry but I think of what you wrote as completely wrong. If I didn't misunderstood.

The size of the memory that the object takes in memory has nothing to do with the size required to serialize it. Memory is handled completely different just because you have entities like classes that are accessed by reference.
There is a reason that the .NET runtime doesn't provide the memory occupied by one single object. It is because it not clear.

If you serialize an object it will serialize all sub-objects. If you think that is the memory that specific instance uses, then what about another instance that is also referencing the sub-objects? Is it also consuming the same amount of memory? What about re-usability since the re-used instance exists only once in memory?

One more thing. Serializing is really expensive as a process. I think that if .NET could give you a number they would have already done it.

Measuring the serialized length is only helpful to know the length that is going over the wire or stored on disk.
AnswerRe: I disagree Pin
Anuja Pawar Indore5-Dec-12 21:55
MemberAnuja Pawar Indore5-Dec-12 21:55 
GeneralRe: I disagree Pin
aSarafian5-Dec-12 22:24
MemberaSarafian5-Dec-12 22:24 
GeneralMy vote of 4 Pin
John Brett21-Oct-12 22:20
MemberJohn Brett21-Oct-12 22:20 
GeneralRe: My vote of 4 Pin
Anuja Pawar Indore21-Oct-12 22:39
MemberAnuja Pawar Indore21-Oct-12 22:39 
GeneralReason for my vote of 5 Nice trick Pin
Tony D Great6-Sep-11 6:56
MemberTony D Great6-Sep-11 6:56 
GeneralRe: Thanks thill :) Pin
Anuja Pawar Indore6-Nov-11 19:04
MemberAnuja Pawar Indore6-Nov-11 19:04 
GeneralCan u be more specific, was not giving correct result or wha... Pin
Anuja Pawar Indore5-Sep-11 21:55
MemberAnuja Pawar Indore5-Sep-11 21:55 
GeneralReason for my vote of 1 not useful Pin
A Bhimani5-Sep-11 21:45
MemberA Bhimani5-Sep-11 21:45 
GeneralReason for my vote of 5 its execleent one Pin
nrkhi4015-Sep-11 18:19
Membernrkhi4015-Sep-11 18:19 
GeneralVery nice, since sizeof() is not available in managed code. ... Pin
CDP180231-Aug-11 22:30
MemberCDP180231-Aug-11 22:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.