I'm new to .NET Platform and got some simple questions about efficiency...
To put you in situation, to say that I'm involved in the writing of a complex regex based lexer for use over the .NET RichTextBox component. Since the lexing is done in realtime at the time the user is typing/editing on the RichTextBox, it is high time consuming, so I'm trying to do the best to get the most efficient code. So here we go:
1. Suppose I declared a simple type variable, int x for example. At some point in the code the value of x is unknown, and I need it to take a specific value. Is more efficient to assign it directly or to check if it has the value I want, and if false, then assign it?
//At this point the value for 'x' is unknown. I need it to take value 0.
OptionA:
x = 0;OptionB:
if (x != 0) { x = 0; }2. I have some methods involved in the lexing of text that are called hundred of times. I have some simple type local variables declared inside these methods to control the processing itself. Since simple types in C# are objects, will be more efficient to declare these variables outside the methods as instance variables to avoid the overload of creating and destructing these objects each time the methods are called? Do really worth in terms of efficiency making visible these variables to the rest of the class (which is not a good thing in terms of coding style)?
Thanks in advance!