List Info

Thread: Properties and Methods




Properties and Methods
user name
2006-02-26 15:45:40
This is a question that I've had knocking around in my head
for a
while.

Here are two Person classes:

public class Person1
{
	private string _firstName = "";

	public void SetFirstName(string firstName)
	{
		this._firstName = firstName;
	}

	public string GetFirstName()
	{
		return this._firstName;
	}

}


public class Person2
{
	private string _firstName = "";

	public string FirstName
	{
		get
		{
			return this._firstName;
		}

		set
		{
			this._firstName = value;
		}
	}

}


As you can see, the first class contains two methods that
are used to
set and get the first name of the Person object.  The second
class
contains a single property FirstName with a getter and
setter.

For the moment let's put aside all questions of getters and
setters
being evil (I do agree with Holub that there should be a
justifiable
reason for exposing a property for reading or writing. 
Let's assume
that there is a valid reason for exposing this property).

My question has to do with the performance of these two
classes and how
the two convert into IL.  What's the difference?  Is there
a
performance difference?  Is there a difference in the
Intermediate
Language?  Is there a perceived benefit to using a property
here rather
than two methods?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C# Developers" group.
To post to this group, send email to CSDevelopersgooglegroups.com
To unsubscribe from this group, send email to
CSDevelopers-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/CSDevelopers
-~----------~----~----~----~------~----~------~--~---

Properties and Methods
user name
2006-02-26 17:18:37
take a look at the IL code
and you shall see that the Property are actualy 2 methods :
get_firstname() and set_firstname()

why do people don't look at the IL code before asking someone else to do ?

btw Properties are a form of abstraction on Get/Set'ers

2006/2/26, themanfromsql <gmail.com">themanfromsqlgmail.com>:

This is a question that I've had knocking around in my head for a
while.

Here are two Person classes:

public class Person1
{
  ; &nbsp; &nbsp; &nbsp;private string _firstName = "&quot;;

   ; &nbsp; &nbsp; public void SetFirstName(string firstName)
 &nbsp; &nbsp; &nbsp;  {
&nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;this._firstName = firstName;
 &nbsp; &nbsp; &nbsp; &nbsp;}

&nbsp; &nbsp; &nbsp;   ;public string GetFirstName()
   ; &nbsp; &nbsp; {
  ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; return this._firstName;
&nbsp; &nbsp; &nbsp; &nbsp; }

}


public class Person2
{
 &nbsp; &nbsp; &nbsp; &nbsp;private string _firstName = "&quot;;

   ; &nbsp; &nbsp; public string FirstName
  ; &nbsp; &nbsp; &nbsp;{
 ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;get
&nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp;{
 ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; return this._firstName;
&nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;  }

&nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;  set
&nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; {
&nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; this._firstName = value;
&nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; }

}


As you can see, the first class contains two methods that are used to
set and get the first name of the Person object.&nbsp; The second class
contains a single property FirstName with a getter and setter.

For the moment let's put aside all questions of getters and setters
being evil (I do agree with Holub that there should be a justifiable
reason for exposing a property for reading or writing.&nbsp; Let's assume
that there is a valid reason for exposing this property).

My question has to do with the performance of these two classes and how
the two convert into IL. &nbsp;What's the difference? &nbsp;Is there a
performance difference? &nbsp;Is there a difference in the Intermediate



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C# Developers" group.
To post to this group, send email to CSDevelopersgooglegroups.com
To unsubscribe from this group, send email to CSDevelopers-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/CSDevelopers
-~----------~----~----~----~------~----~------~--~---

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )