Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
VFP inlist() in c#
Message
From
02/06/2008 08:44:11
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01320903
Message ID:
01320937
Views:
44
>>>if there any similar vfp inlist() command in C#?
>>>
>>>
>>>for eg.
>>>
>>>VFP Code:
>>>
>>>IF INLIST(postCode,'001','002','003')
>>>...
>>>ENDIF
>>>
>>>
>>>
>>>Thanks.
>>
>>As far as I know, there isn't. You can easily write one - and overload it for other types
>>
>>static bool InList(string s, params string[] list)
>>{
>>	for (int i = list.Length; --i >= 0; )
>>		if (s.CompareTo(list[i]) == 0)
>>			return true;
>>	return false;
>>}
>>
>
>
>Short and sweet. Just wondering, is there a reason for traversing the parameter list backwards?
_
Yes
(1) I do not have to access list.Length each time
(2) Comparing to zero is - I believe - faster
(a) ildasm loads a constant zero on the stack, then compares, then branches
load i
load zero // no access to list.Length each time - just constant zero
compare
branch (true/false)

(b) that code is likely to be optimized by the JIT


ps: (2) a
Comparing zero/not zero does not need to load constant zero on the stack

There's asm opcode
- brtrue (branch when not equal to zero)
- brfalse (branch when zero)

So this would even be faster

load i
branch (true or false)
static bool Inlist(string s, params string[] list)
{
	for ( int i = list.Length; i-- != 0; )
		if (s.CompareTo(list[i]) == 0)
			return true;
	return false;
}
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform