Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
No Overload takes 5 arguments
Message
De
23/09/2013 06:50:19
 
 
À
23/09/2013 06:23:57
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01583574
Message ID:
01583864
Vues:
30
>>On an afterthought
>>
>>>>And if you use 'ref' you are forced to instantiate the objects before the call otherwise it won't compile ; 'out' avoids that....
>>
>>Nowhere is it written that you have to instantiate the objects before the call - only that they have to be initialized
>>
>>A null value is ok
>>
>>	static class Test_RefOut
>>	{
>>		internal static void Go()
>>		{
>>			Dictionary<string, string> dict = null;
>>
>>			int x = GetDictByRef(ref dict);
>>
>>			Console.WriteLine("result : {0}", dict);
>>		}
>>
>>		static int  GetDictByRef(ref Dictionary<string, string> dict)
>>		{
>>			//dict = new Dictionary<string, string>();
>>			return 2;
>>		}
>>		
>>	}
>>
True. OK - back to message 1583810:
>" value types : depending on the size of the value type "
>
>What if the parameter is a biggish structure. More bytes to pass there. e.g.
 public struct LotsOfDoubles
>    {
>        public double a, b, c, d, e, f, g, h, i, j;
>    }
OK,
	public struct LotsOfDoubles
	{
		public double a, b, c, d, e, f, g, h, i, j;
	}

	static class Test_RefOut
	{
		internal static void Go()
		{
			LotsOfDoubles lod = new LotsOfDoubles();


			int x = GetLotsOfDoublesByRef(ref lod);

			Console.WriteLine("result : {0}", lod);
		}

		static int GetLotsOfDoublesByRef(ref LotsOfDoubles lod)
		{
			lod = new LotsOfDoubles();
			lod.a = 1.4;
			lod.b= 1.4;
			lod.c = 1.4;
			lod.d = 1.4;
			lod.e = 1.4;
			lod.f = 1.4;
			lod.g = 1.4;
			lod.h = 1.4;
			lod.i = 1.4;
			lod.j = 1.4;

			//dict = new Dictionary<string, string>();
			return 2;
		}
		
	}
When calling: The local address of LotsOfDoubles is put onto the stack
In the method called. the address is treated as a struct - the same amount of work it would take to initialize if the calling method


No waste - It's only the address that is passed (like the address of an object on the heap would be passed)



ildasm
So, 
  for each field
       load the address of the struct on the stack
       load a constant (1.4 here) on the stack
       store the constant in field x
  

.method private hidebysig static int32  GetLotsOfDoublesByRef(valuetype GregoryAdam.Test.Tests.LotsOfDoubles& lod) cil managed
{
  // Code size       164 (0xa4)
  .maxstack  2
  .locals init ([0] int32 CS$1$0000)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  initobj    GregoryAdam.Test.Tests.LotsOfDoubles
  IL_0008:  ldarg.0
  IL_0009:  ldc.r8     1.3999999999999999
  IL_0012:  stfld      float64 GregoryAdam.Test.Tests.LotsOfDoubles::a
  IL_0017:  ldarg.0
  IL_0018:  ldc.r8     1.3999999999999999
  IL_0021:  stfld      float64 GregoryAdam.Test.Tests.LotsOfDoubles::b
  IL_0026:  ldarg.0
  IL_0027:  ldc.r8     1.3999999999999999
  IL_0030:  stfld      float64 GregoryAdam.Test.Tests.LotsOfDoubles::c
  IL_0035:  ldarg.0
  IL_0036:  ldc.r8     1.3999999999999999
  IL_003f:  stfld      float64 GregoryAdam.Test.Tests.LotsOfDoubles::d
  IL_0044:  ldarg.0
  IL_0045:  ldc.r8     1.3999999999999999
  IL_004e:  stfld      float64 GregoryAdam.Test.Tests.LotsOfDoubles::e
  IL_0053:  ldarg.0
  IL_0054:  ldc.r8     1.3999999999999999
  IL_005d:  stfld      float64 GregoryAdam.Test.Tests.LotsOfDoubles::f
  IL_0062:  ldarg.0
  IL_0063:  ldc.r8     1.3999999999999999
  IL_006c:  stfld      float64 GregoryAdam.Test.Tests.LotsOfDoubles::g
  IL_0071:  ldarg.0
  IL_0072:  ldc.r8     1.3999999999999999
  IL_007b:  stfld      float64 GregoryAdam.Test.Tests.LotsOfDoubles::h
  IL_0080:  ldarg.0
  IL_0081:  ldc.r8     1.3999999999999999
  IL_008a:  stfld      float64 GregoryAdam.Test.Tests.LotsOfDoubles::i
  IL_008f:  ldarg.0
  IL_0090:  ldc.r8     1.3999999999999999
  IL_0099:  stfld      float64 GregoryAdam.Test.Tests.LotsOfDoubles::j
  IL_009e:  ldc.i4.2
  IL_009f:  stloc.0
  IL_00a0:  br.s       IL_00a2
  IL_00a2:  ldloc.0
  IL_00a3:  ret
} // end of method Test_RefOut::GetLotsOfDoublesByRef
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform