-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I thought he wanted the type to be exact, since I was just translating
his first line into how I would do it. But after seeing your reply, I
read Ron's code further down, and I realized that:
1. Ron apparently just wanted to check if he could cast it to a DropDownList
2. The DropDownList is already at the bottom of the hierarchy, so "is"
will give him a true only if the instance is indeed a DropDownList anyway...
So yeah, "is" does satisfy the requirements, and much more concise to
type :)
About the performance thing (where did you get this StopWatch class?
Seems quite useful), how about putting the typeof() outside the loop?
Guess that'll make a difference! :)
Greg Ewing [MVP] wrote:
| So Nicholas and Ray, what do you guys have against the is operator?
<g> is
| seems to be a whole lot faster than GetType and typeof(). Here's what I
| found for 1000 iterations:
|
| Using the is operator took 00:00:00.000025 1
| Using GetType() took 00:00:00.000205 3
|
| Here's the code I used. It is using Whidbey so that might affect
things but
| I soft of doubt it would make GetType 10 times slower like I saw.
|
| [STAThread]
| static void Main(string[] args)
| {
| Control ctl = new DropDownList();
| Stopwatch sw = new Stopwatch();
| int j = 0;
| sw.Start();
| for (int i = 0; i < 1000; i++)
| {
| if (ctl is System.Web.UI.W ebControls.Drop DownList)
| {
| j++;
| //Console.WriteLi ne("ctl is a DropDownList");
| }
| else
| {
| j++;
| //Console.WriteLi ne("ctl is NOT a DropDownList");
| }
| }
| sw.Stop();
| Console.WriteLi ne("Using the is operator took " + sw.Elapsed);
| sw.Start();
| for (int i = 0; i < 1000; i++)
| {
| if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))
| {
| j++;
| //Console.WriteLi ne("ctl is a DropDownList");
| }
| else
| {
| j++;
| //Console.WriteLi ne("ctl is NOT a DropDownList");
| }
| }
| sw.Stop();
| Console.WriteLi ne("Using GetType() took " + sw.Elapsed);
| Console.Read();
| }
|
- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird -
http://enigmail.mozdev.org
iD8DBQE/oTP4wEwccQ4rWPg RAnSWAJ9jRyA0E+ qpCiLpcY8Ap9fty +DoJgCdFKKo
JeJ3RhBNh9zYoMm lUOdmXrw=
=U+E1
-----END PGP SIGNATURE-----