Type Inference

Una caratteristica molto interessante del framework è l'inferenza dei tipi, cioè la capacità del compilatore di capire quali sono i tipi da usare senza che il programmatore li dichiari esplicitamente. Questa caratteristica è stata notevolmente utilizzata nel framework 3.0, ma è presente anche nel 2.0.

Ad esempio:

class FooClass

{

public void Test() {    

Foo<int, string>(10, "aaa");

Foo(10, "aaa");

Foo("bbb", 10);

}

 

void Foo<I, J>(I parA, J parB) {

Console.WriteLine("First method");

}

}

Nel metodo Test, le chiamate al metodo Foo sono tutte lecite. Nel primo caso vengono esplicitati i parametri di tipo, nel secondo il compilatore "conclude" che i tipi I e J sono rispettivamente int e string, mentre nel terzo caso I e J sono rispettivamente string e int. L'inferenza dei tipi rende meno prolisso il codice ma nasconde delle insidie.

Come più volte ha dimostrato Adrian nei suoi post, quando si lascia al compilatore l'interpretazione del nostro codice, non sempre le scelte intraprese sono quelle che noi ci aspetteremo. Ad esempio:

class FooClass

{

public void Test() {

Foo<int, string>(10, "aaa");

Foo(10, "aaa");

}

 

void Foo<I, J>(I parA, J parB) {

Console.WriteLine("First method");

}

 

void Foo<I, J>(int parA, string parB){

Console.WriteLine("Second method");

}

}

In questo caso l'output generato chiamando il metodo Test sarà:

First method

Second method

Quindi quando specifichiamo i parametri di tipo int e string il compilatore sceglie il primo dei due metodi, anche se a qualcuno sembrerebbe più logico il secondo. Se non specifichiamo i parametri di tipo, il compilatore sceglie il secondo dei metodi.

posted @ lunedì 27 novembre 2006 13:19

Print

Comments on this entry:

# re: Type Inference

Left by film izle at 16/12/2009 23:06
Gravatar
Thanks admin

film izle

# re: Type Inference

Left by JULIETTESims at 02/11/2010 13:53
Gravatar
We should receive custom dissertation or dissertation expert. And to demonstrate that you are the professional, you have to use thesis writing service.

# re: Type Inference

Left by sikiş at 29/03/2011 01:01
Gravatar
thanks for sharing i love this idea and i will check everytime your post:)) see ya again in new post

# re: Type Inference

Left by sikiş at 24/05/2011 20:45
Gravatar
thanks for sharing i love this idea and i will check everytime your post:))
see ya again in new post.wish you continued success

# re: Type Inference

Left by sikiş at 28/06/2011 14:08
Gravatar
thanks for sharing i love this idea and i will check everytime your post:))
see ya again in new post.wish you continued success

# re: Type Inference

Left by MorseAnnabelle at 24/07/2012 09:55
Gravatar
Get use of Primewritings company and buy a research paper in order receive help with research paper or purchase term paper from this agency.As only skilled writers cooperate with that agency you will gain amazing academic writing services.All you have to to it to purchase college research paper at that paper writing agency.

# re: Type Inference

Left by Definition Essay at 21/03/2013 12:23
Gravatar
Our firm has been in the writing industry for a long time, so if you are seeking where to obtain high quality Abortion Essay, just come to us.
Comments have been closed on this topic.
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011