Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Why a string comes back as null
Message
From
04/12/2008 04:49:48
 
 
To
03/12/2008 13:02:11
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Application:
Desktop
Miscellaneous
Thread ID:
01365266
Message ID:
01365455
Views:
8
>Both great ideas. No reason other than my not being used to using out parameters. I wasn't aware a null comparison was faster so I learned something too. Thanks
>Tim

Tim,

This piece uses null
class test
{
	static void Main(string[] args)
	{
		string fileName = null;
		int i = 0;

		while ((fileName = PopQueue()) != null)
		{
			i++;
		}
	}
	static string PopQueue()
	{
		return null;
	}

}
Ildasm gives this:look at instruction IL_000d: it branches if the return of popqueue() is not null
.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       16 (0x10)
  .maxstack  2
  .locals init ([0] int32 i)
  IL_0000:  ldc.i4.0
  IL_0001:  stloc.0
  IL_0002:  br.s       IL_0008
  IL_0004:  ldloc.0
  IL_0005:  ldc.i4.1
  IL_0006:  add
  IL_0007:  stloc.0
  IL_0008:  call       string test::PopQueue()
  IL_000d:  brtrue.s   IL_0004
  IL_000f:  ret
} // end of method test::Main
Now with ""
class test
{
	static void Main(string[] args)
	{
		string fileName = null;
		int i = 0;

		while ((fileName = PopQueue()) != "")
		{
			i++;
		}
	}
	static string PopQueue()
	{
		return null;
	}

}
ildasm: look at
000d: load string
0012: a call
0017: branch if != 0

So we have an extra load + call
.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       26 (0x1a)
  .maxstack  2
  .locals init ([0] int32 i)
  IL_0000:  ldc.i4.0
  IL_0001:  stloc.0
  IL_0002:  br.s       IL_0008
  IL_0004:  ldloc.0
  IL_0005:  ldc.i4.1
  IL_0006:  add
  IL_0007:  stloc.0
  IL_0008:  call       string test::PopQueue()
  IL_000d:  ldstr      ""
  IL_0012:  call       bool [mscorlib]System.String::op_Inequality(string,
                                                                   string)
  IL_0017:  brtrue.s   IL_0004
  IL_0019:  ret
} // end of method test::Main
One may argue that in your example the difference will be about zilch. And they would be right. But I look at it as a habit. Why not reach for the faster way - the cost of programming is the same

In addition to that there is something else I keep in mind that will make a program perform a bit faster: When possible I loop towards zero.
The difference may be 10% or so for the loop instructions. Maybe not a big deal, but I like it

Consider this

(1) the 'slower' way
class test
{
	static void Main(string[] args)
	{
		int i;
		int[] array = new int[20];

		for (i = 0; i < array.Length; i++)
		{
		}
	}
}
ildasm: comments inside
.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       23 (0x17)
  .maxstack  2
  .locals init ([0] int32 i,
           [1] int32[] 'array')
  IL_0000:  ldc.i4.s   20
  IL_0002:  newarr     [mscorlib]System.Int32
  IL_0007:  stloc.1
  IL_0008:  ldc.i4.0
  IL_0009:  stloc.0
  IL_000a:  br.s       IL_0010
  IL_000c:  ldloc.0
  IL_000d:  ldc.i4.1
  IL_000e:  add
  IL_000f:  stloc.0
  IL_0010:  ldloc.0    // load i
  IL_0011:  ldloc.1  // load array
  IL_0012:  ldlen    // call length of array
  IL_0013:  conv.i4  // convert to int32   ???
  IL_0014:  blt.s      IL_000c      // if < goto 000c:
  IL_0016:  ret
} // end of method test::Main
loop towards zero
class test
{
	static void Main(string[] args)
	{
		int i;
		int[] array = new int[20];

		for (i = array.Length; --i >= 0; )
		{
		}
	}
}
ildasm
.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       21 (0x15)
  .maxstack  2
  .locals init ([0] int32 i,
           [1] int32[] 'array')
  IL_0000:  ldc.i4.s   20
  IL_0002:  newarr     [mscorlib]System.Int32
  IL_0007:  stloc.1
  IL_0008:  ldloc.1
  IL_0009:  ldlen              // only done once
  IL_000a:  conv.i4         // only done once
  IL_000b:  stloc.0
  IL_000c:  ldloc.0
  IL_000d:  ldc.i4.1    // load constant 1
  IL_000e:  sub  //  ie --i
  IL_000f:  dup 
  IL_0010:  stloc.0   // store in i
  IL_0011:  ldc.i4.0 // load zero
  IL_0012:  bge.s      IL_000c  // if ( i >= 0 ) goto 000c
  IL_0014:  ret
} // end of method test::Main
Have a look at this - I liked it ( I think I posted the link before) http://msdn.microsoft.com/en-us/library/ms973852.aspx
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform