Click here to Skip to main content
15,901,373 members
Home / Discussions / C#
   

C#

 
GeneralRe: Identify caller of a function Pin
User 665826-Nov-05 13:27
User 665826-Nov-05 13:27 
AnswerRe: Identify caller of a function Pin
leppie26-Nov-05 22:50
leppie26-Nov-05 22:50 
GeneralRe: Identify caller of a function Pin
User 665827-Nov-05 6:01
User 665827-Nov-05 6:01 
QuestionUnable to see underlined shorcuts in main menu Pin
TheBlindWatchmaker26-Nov-05 7:38
TheBlindWatchmaker26-Nov-05 7:38 
AnswerRe: Unable to see underlined shorcuts in main menu Pin
Dave Kreskowiak26-Nov-05 8:19
mveDave Kreskowiak26-Nov-05 8:19 
GeneralRe: Unable to see underlined shorcuts in main menu Pin
TheBlindWatchmaker26-Nov-05 8:32
TheBlindWatchmaker26-Nov-05 8:32 
Questionproperties vs public fields Pin
hypermegachi26-Nov-05 7:04
hypermegachi26-Nov-05 7:04 
AnswerRe: properties vs public fields Pin
Colin Angus Mackay26-Nov-05 7:23
Colin Angus Mackay26-Nov-05 7:23 
hypermegachi wrote:
all i could find is that the property version is slower because it has to invoke a method (albeit it'll probably never matter)...is there any reason i should encapsulate this, given i will very frequently be assigning new references?


The encapsulation means that you don't have to re-write lots of code, if at some point in the future you discover that you need to do some extra work in a getter or setter.

Getter example: You might decide later that it would be better to leave the field uninitialised and then have your property initialise it when it is first used. This is called a lazy-lookup. The value can be constructed, but it is an expensive operation to do so, and the usage patterns show that the value is not often needed - so why create it needlessly. So, essentially your property checks the field, if the field is null it creates the value and stores it in the field and returns the new value. If the field already contains a value then that is returned.

Setter example: You discover some time in the future that when you set the property that you need to carry out some other action (an analogy would be like a trigger in the database). So, since you have a property already, you can now put the extra functionality in without having to completely break your application.

Another Setter example: You could throw an exception if a value that was being set is out of range. Thus keeping the object in a consistent state. If you just exposed the field you would never know if some other object set the field to an invalid value and this is a potential source of bugs, especially the nasty sort that rear their head further on past the point that the bug was actually introduced. These are a real pain to track down and anyone having to maintain your code will not thank you for it.

In general, anything outside the class should not need to know anything about the internals of the class. If your class is exposing field members then you are exposing the internals of the class.

Finally, as to the view that using a property will incure the penalty of a method call then you should know that the compiler is smarter than you think. It will often inline properties and small methods for you so there is no penalty at all. If you are using VS2005 you can see this for yourself as there is support for debugging release assemblies and you can see optimisations.

Does this help?


My: Blog | Photos

"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

-- modified at 13:24 Saturday 26th November, 2005


GeneralRe: properties vs public fields Pin
hypermegachi26-Nov-05 12:13
hypermegachi26-Nov-05 12:13 
GeneralRe: properties vs public fields Pin
Colin Angus Mackay26-Nov-05 13:53
Colin Angus Mackay26-Nov-05 13:53 
GeneralRe: properties vs public fields Pin
hypermegachi27-Nov-05 6:43
hypermegachi27-Nov-05 6:43 
GeneralRe: properties vs public fields Pin
Colin Angus Mackay27-Nov-05 13:24
Colin Angus Mackay27-Nov-05 13:24 
GeneralRe: properties vs public fields Pin
hypermegachi27-Nov-05 16:53
hypermegachi27-Nov-05 16:53 
GeneralRe: properties vs public fields Pin
Colin Angus Mackay27-Nov-05 20:26
Colin Angus Mackay27-Nov-05 20:26 
GeneralRe: properties vs public fields Pin
hypermegachi28-Nov-05 3:58
hypermegachi28-Nov-05 3:58 
QuestionUrgent:Marshalling BSTR* Pin
rana7426-Nov-05 6:23
rana7426-Nov-05 6:23 
QuestionISR in C# Pin
mostafa.hk26-Nov-05 5:21
mostafa.hk26-Nov-05 5:21 
AnswerRe: ISR in C# Pin
Stanciu Vlad26-Nov-05 5:39
Stanciu Vlad26-Nov-05 5:39 
Questionregex Pin
Sam 200626-Nov-05 5:12
Sam 200626-Nov-05 5:12 
AnswerRe: regex Pin
Guffa26-Nov-05 8:50
Guffa26-Nov-05 8:50 
AnswerRe: regex Pin
Curtis Schlak.26-Nov-05 10:30
Curtis Schlak.26-Nov-05 10:30 
QuestionControl apperance in VS 2005 Pin
ON7AMI26-Nov-05 4:17
ON7AMI26-Nov-05 4:17 
AnswerRe: Control apperance in VS 2005 Pin
Sam 200626-Nov-05 5:17
Sam 200626-Nov-05 5:17 
GeneralRe: Control apperance in VS 2005 Pin
ON7AMI27-Nov-05 7:22
ON7AMI27-Nov-05 7:22 
Questioninserting images in word document from c#? Pin
Hiral Patel26-Nov-05 2:52
Hiral Patel26-Nov-05 2:52 

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.