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 11.19

Print

Comments on this entry:

# re: Type Inference

Left by sohbet at 08/02/2009 13.58
Gravatar

# re: Type Inference

Left by sohbet at 12/07/2009 19.46
Gravatar

# sikiş

Left by sikiş izle at 03/12/2009 17.56
Gravatar

# re: Type Inference

Left by film izle at 16/12/2009 21.06
Gravatar
Thanks admin

film izle

# re: Type Inference

Left by DEE29Whitley at 29/07/2010 21.36
Gravatar
People all over the world take the credit loans in various banks, just because this is comfortable.

# re: Type Inference

Left by JanetMccormick28 at 05/08/2010 13.00
Gravatar
I opine that people would make better their writing ability ordering term paper titles at academic custom essay writing service. Because a great custom writing service can write custom essays on any branch of study.

# re: Type Inference

Left by MarvaPugh18 at 10/08/2010 6.54
Gravatar
Technologies evolve every single day. Thus, the term paper composing services emerged. Hence, university students feel free purchasing custom essay.

# re: Type Inference

Left by Patterson34Juana at 15/09/2010 14.21
Gravatar
My good friend said me that he used the social bookmarking submission service for his site optimization. Thus, I would like to try to do the same.

# re: Type Inference

Left by OneillChandra27 at 23/09/2010 1.15
Gravatar
Some men guess that is risky to buy essays. Nonetheless, I know that writing corporations offer premium standards only, just because their face relies on that.

# re: Type Inference

Left by POTTER27KATHARINE at 05/10/2010 1.45
Gravatar
Very often even advanced university students cannot to cope with complicated assignments. What should they do? My intuition hints that they ought to order course work writing.

# re: Type Inference

Left by Dejesus22Estella at 29/10/2010 3.53
Gravatar
Result aimed college students worry about academic grades, hence they try to utilize the online term paper service, which supposes to be very important.

# re: Type Inference

Left by JULIETTESims at 02/11/2010 11.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 Rollins24Penny at 12/11/2010 1.09
Gravatar
If you like to get a success, you would complete the well done philosophy essays. The professional essay should be a proper issue for custom written essays performing, I do opine.

# re: Type Inference

Left by HannahRoss25 at 13/11/2010 1.24
Gravatar
Numbers of men just are talking about supreme quality! I guarantee that to buy essay seems to be safely.

# re: Type Inference

Left by casustelefondinleme at 24/12/2010 15.10
Gravatar
profesyonel casus telefondinleme programı
gizli telefondinleme yazılımı

# re: Type Inference

Left by sikiş at 28/03/2011 22.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 17.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 11.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

Your comment:



 (will not be displayed)


 
 
 
Please add 7 and 5 and type the answer here:
 

Live Comment Preview:

 
«febbraio»
domlunmarmergiovensab
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910