Вы находитесь на странице: 1из 16

Visual Studio.

NET Best Articles

Visual C# 2008 Breaking Changes


The following table lists all the breaking changes in Visual C# 2008 Service Pack 1 that might impact an application that was create in the original release version of Visual C# 2008 or in Visual C# 200!" Change number Category Issue Description %n Visual C# 2008 an earlier& t$pe inference causes arra$s of pointer t$pes to be e'clu e from the metho overloa resolution process" %n the following co e& the Visual C# 200! compiler selects the non(generic version of Test because the generic version of Test is e'clu e from consi eration because of its t$pe parameter of int*[]" %n Visual C# 2008& the generic version of Test is selecte " Cop$ Co e

using System.Collections.Generic;
T$pe inference is now inclu e on arra$s of pointer t$pes in metho overloa resolution"

#verloa resolution

unsafe class Program { static void Main( { !"numera#le$int*[]% y & null; Test(y ; ' (( Selected #y )isual C* +,,-. static void Test$S%(!"numera#le$S% . { ' (( Selected #y )isual C* +,,/. static void Test(o#0ect o { ' '
%n the original release version of Visual C# 2008 an earlier versions& it is possible to efine an e'plicit implementation of an

in e'er in which the implementation has a params parameter but the interface efinition oes not" This construction is contrar$ to the specification" %n Visual C# 2008 SP1& this construction pro uces Compiler +rror CS0)**& as shown in the following co e" Cop$ Co e

%n e'ers

Compiler now pro uces error CS0)** for in e'ers an properties in a ition to metho s"

interface ! { int t1is[int[] 2] { set; ' ' class 3ase 4 ! { (( Produces CS,5664 int !.t1is[2arams int[] 2] set { ' '
'

-ullable Compiler now t$pes an ?? correctl$ evaluates e'pressions e'pressions in which nullable variables are compare to themselves"

%n the original release version of Visual C# 2008& the following co e compiles an outputs .false. at run time" %n Visual C# 2008 Service Pack 1& Compiler /arning 0level ,1 CS1218 is pro uce an .true. is output" Cop$ Co e

Ravi Varma Thumati

Page 1

11/19/2013

Visual Studio.NET Best Articles

static class Program { static void Main( { int7 . & null; #ool y & . && .; Console.8rite9ine(y ; ' '
) try !inally in +'ecution of neste iterators !inally blocks from iterators that have brea" statements is change " %n the original release version of Visual C# 2008& the following co e e'ecutes the outer !inally two times" %n Visual C# 2008 SP1& the outer !inally is e'ecute one time" Cop$ Co e

using System; using System.Collections; using System.Collections.Generic; 2u#lic class Test { 2u#lic static void Main( { Console.8rite9ine(:in main: ; foreac1 (int i in Get!nts( { Console.8rite9ine(:in foreac1: ; #rea;; ' ' static !"numera#le$int% Get!nts( { Console.8rite9ine(:in Get!nts: ; <1ile (true { Console.8rite9ine(:in <1ile: ; try { Console.8rite9ine(:in outer try: ; try { Console.8rite9ine(:in inner try #efore yield: ; yield: ; yield return =; Console.8rite9ine(:in inner try after #rea;; ' finally { Console.8rite9ine(:in inner finally: ; ' ' finally { Console.8rite9ine(:in outer finally: ;

Ravi Varma Thumati

Page 2

11/19/2013

Visual Studio.NET Best Articles

' ' '

'

%n the original release version of Visual C# 2008& the following co e outputs 2& 0" The line Console.8rite9ine(e.Com2ile( (default(T ; outputs 3ero because S is incorrectl$ being bo'e " %n Visual C# 2008 SP1& no bo'ing occurs& an the program outputs 2& 2" Cop$ Co e

using System; using System.9in>; using System.9in>.".2ressions; class Program { static void Main( { Test$S%( ; ' static void Test$T%( <1ere T 4 ! { %ncorrect bo'ing of ".2ression$?unc$T@ int%% e & . &% ..SetA( B metho e'pressions in ..A; e'pression trees no (( Co #o.ing in SP=4 longer occurs" Console.8rite9ine(e.Com2ile( (default(T ; ' ' interface ! { int A { get; ' int SetA( ; ' struct S 4 ! { 2u#lic int A { get; 2rivate set; ' 2u#lic int SetA( { A & D; return ,; ' '
* #b4ect initiali3ers %nitiali3ation of value t$pes in ob4ect initiali3ers has been correcte " %n the original release version of Visual C# 2008& the local variable # in the following e'ample is not initiali3e correctl$& an its member A has a value of 3ero" %n Visual C# 2008 SP1& S.A is correctl$ initiali3e to 1 in both ne# e'pressions" Cop$ Co e

+'pression trees

using System; using System.9in>; using System.9in>.".2ressions; class Program {

Ravi Varma Thumati

Page 3

11/19/2013

Visual Studio.NET Best Articles

static void Main( { Test$S%( ; ' static void Test$T%( <1ere T 4 !@ ne<( { var a & ne< T( ; a.A & =; Console.8rite9ine(a.A ; var # & ne< T { A & = '; Console.8rite9ine(#.A ;

' '

interface ! { int A { get; set; ' ' struct S 4 ! { 2u#lic int A { get; set; ' ' (( Eriginal release version of )isual C* +,,- out2ut4 = , (( )isual C* +,,- SP= out2ut4 = =
%n the original release version of Visual C# 2008& null literals are in some cases allowe to be converte to enum values" %n Visual C# 2008 SP1& Compiler +rror CS1!02 an Compiler +rror CS1!0, are pro uce if $ou tr$ to o this& as shown in the following e'ample" Cop$ Co e

enum My"num { Fero & ,@ Ene & = '


2 T$pe conversions -ull literals are no longer convertible to enum values"

class MyClass { ' class Program { static void Main(string[] args { (( Produces CS=/,+ and CS=/,G4 Test((MyClass null ; ' static void Test(My"num . { System.Console.8rite9ine(. ; '

'
8 +'pression trees %nvali e'pression tree now throws the correct e'ception"

%n the original release version of Visual C# 2008& an e'pression tree that contains a metho call to a metho that is not on the specifie t$pe throws a System.Security.Veri!icationE$ception " %n Visual

Ravi Varma Thumati

Page 4

11/19/2013

Visual Studio.NET Best Articles


C# 2008 SP1& a System.ArgumentE$ception is thrown& as shown in the following co e" Cop$ Co e

using System; using System.Heflection; using System.9in>; using System.9in>.".2ressions; class Program { 2u#lic struct S { ' static void Main( { Ty2e t & ty2eof(System."num ; Met1od!nfo m & t.GetMet1od(:GetTy2eCode: ; Parameter".2ression 2 & ".2ression.Parameter(ty2eof(S @ :s: ; ".2ression$?unc$S@ Ty2eCode%% e & ".2ression.9am#da$?unc$S@ Ty2eCode%%( (( T1ro<s System.Irgument".ce2tion in )isual C* +,,- SP=4 ".2ression.Call(2@ m @ 2 ; ?unc$S@ Ty2eCode% f & e.Com2ile( ; (( T1ro<s System.Security.)erification".ce2tion in t1e (( original release version of )isual C* +,,-4 Console.8rite9ine(f(ne< S( ; ' '
5 6ttributes CharSet"7nico e is now propagate to helper t$pes that C# generates for fi'e arra$ fiel s" The C# compiler generates helper t$pes to encapsulate fi'e arra$s" %n the original release version of Visual C# 2008 an earlier& the la$out of the arra$ is alwa$s 6-S%& even if the Struct%ayout attribute specifies CharSet"7nico e" There was no wa$ to change that in C# source co e" %n Visual C# 2008 SP1& whatever CharSet value is specifie in the Struct%ayout attribute is use to construct the helper class& as shown in the following co e" Cop$ Co e

using System; using System.Huntime.!ntero2Services; [Struct9ayout(9ayoutJind.Se>uential@ C1arSet & C1arSet.Knicode ] unsafe struct Test { 2u#lic fi.ed c1ar C1ars[-]; ' class Program { static void Main(string[] args { ' '

Eriginal release version of )isual C* +,,- MS!94 .class se>uential ansi sealed nested 2u#lic #eforefieldinit L$C1ars%eMM?i.ed3uffer,L e.tends [mscorli#]System.)alueTy2e

Ravi Varma Thumati

Page 5

11/19/2013

Visual Studio.NET Best Articles

(( ... ' (( end of class L$C1ars%eMM?i.ed3uffer,L )isual C* +,,- SP= MS!94 .class se>uential unicode sealed nested 2u#lic #eforefieldinit L$C1ars%eMM?i.ed3uffer,L e.tends [mscorli#]System.)alueTy2e { (( . . . ' (( end of class L$C1ars%eMM?i.ed3uffer,L
%n the original release version of Visual C# 2008& it is possible for a stac"alloc allocation to fail without causing an e'ception" This is because of an unchecke mul instruction in the generate 8icrosoft interme iate language 08S%91 when the length of the arra$ is multiplie b$ the si3e of each element" %n Visual C# 2008 SP1& a mul"ovf instruction is generate instea of a mul& so overflows pro uce a System.&'er!lo#E$ ception when the allocation is attempte at run time" Cop$ Co e

10

#verflow checking

stac"alloc now performs an overflow check"

class Program { static void Main(string[] args { int var & ,.5,,,,,,,; unsafe { (( ,.5,,,,,,, * siNeof(int does not fit in an int. int* listS & stac;alloc int[var]; (( )isual C* +,,- SP=4 System.Everflo<".ce2tion. listS[,] & /; (( Eriginal release version of )isual C* +,,-4 (( System.CullHeference".ce2tion. ' ' '
%n 9%-; :uer$ e'pressions over non(generic collections such as System.Collections.Array%ist& the !rom clause of the :uer$ is rewritten b$ the compiler to inclu e a call to the Cast(T) operator" Cast(T) converts all element t$pes to the t$pe specifie in the !rom clause in the :uer$" %n a ition& in the original release version of Visual C# 2008& the Cast(T) operator also performs some value t$pe conversions an user( efine conversions" <owever& these conversions are performe b$ using the System.Con'ert class instea of the stan ar C# semantics" These conversions also cause significant performance issues in certain scenarios" %n Visual C# 2008 SP1& the Cast(T) operator is mo ifie to throw an In'alidCastE$ception for numeric value t$pe an user( efine conversions" This change eliminates both the non(stan ar C# cast semantics an the performance issue" This change is illustrate in the following e'ample" Cop$ Co e

11

Stan ar :uer$ operators

;ueries over non( generic collections now use stan ar C# cast semantics"

using System; using System.9in>; class Program { 2u#lic struct S { ' static void Main(

Ravi Varma Thumati

Page 6

11/19/2013

Visual Studio.NET Best Articles

var floats & ne< float[] { +.Df@ G.=f@ 5./f '; var ints & from int i in floats select i;

(( )isual C* +,,- SP= t1ro<s !nvalidCast".ce2tion. foreac1 (var v in ints Console.8rite(:{,' :@ v.ToString( ; (( T1e original release version of )isual C* +,,(( com2iles and out2uts G G 5

' '

=reaking Changes in the #riginal >elease Version of Visual C# 2008 The following table lists all the breaking changes in the original release version of Visual C# 2008 that might prevent an application that was create in Visual C# 200! from compiling& or that might change its run(time behavior" Change number Category Issue Description 6 literal 0 is implicitl$ convertible to an$ enum t$pe" %n Visual C# 200! an earlier versions of the compiler& there are also some constant e'pressions that evaluate to 0 that can implicitl$ convert to an$ enum t$pe& but the rule that etermines which of these e'pressions are convertible is unclear" %n Visual C# 2008& all constant e'pressions that are e:ual to 0 can be implicitl$ converte to an$ enum t$pe" This coul cause some changes in the behavior of e'isting co e& such as metho overloa resolution that relies on the absence of this implicit conversion" The following co e compiles successfull$ on Visual C# 200! an earlier compilers& resolving the metho invocation on the short value onl$ to the int overloa " %n Visual C# 2008& this invocation is ambiguous because the short value is also implicitl$ convertible to "" %n Visual C# 2008& the behavior is change to allow the conversion of an$ constant e'pression that evaluates to 3ero" Cop$ Co e

12

Conversion of an$ constant T$pe e'pression with a conversions value of 3ero to enum is now allowe "

2u#lic enum " { Fero & ,@ Ene & =@ ' class I { 2u#lic I(string s@ o#0ect o { System.Console.8rite9ine(:{,' &% I(o#0ect :@ s ; ' 2u#lic I(string s@ " e { System.Console.8rite9ine(:{,' &% I("num " :@ s ; '

'

class 3 { static void Main( { I a= & ne< I(:,:@ , ; I a+ & ne< I(:=:@ = ; I aG & ne< I(:(int ".Fero:@ (int ".Fero ; I a5 & ne< I(:(int ".Ene:@ (int ".Ene ;

Ravi Varma Thumati

Page 7

11/19/2013

Visual Studio.NET Best Articles

' ' )isual C* +,,/ out2ut4 , &% I("num " = &% I(o#0ect (int ".Fero &% I(o#0ect (int ".Ene &% I(o#0ect )isual C* +,,- out2ut4 , &% I("num " = &% I(o#0ect (int ".Fero &% I("num " (int ".Ene &% I(o#0ect
%n Visual C# 200!& no error is pro uce if an assembl$ contains two S$stem">untime"CompilerServices"T$pe?orwar e To attributes that target the same t$pe" %n Visual C# 2008& Compiler +rror CS02,5 is pro uce & as shown in the following e'ample" Cop$ Co e

(( Class=.cs (( Causes CS,DGO4 [assem#ly4 System.Huntime.Com2ilerServices.Ty2e?or<ardedTo(ty2eo f(Test ] [assem#ly4 System.Huntime.Com2ilerServices.Ty2e?or<ardedTo(ty2eo f(Test ]


1, 6ttributes +rror now occurs when the same T$pe?orwar e To attribute is present two times in one assembl$"

2u#lic class Test { 2u#lic static int Main( { Test f & ne< Test( ; return f.get)alue( ; ' ' (( 9i#rary=.cs 2u#lic class Test { 2u#lic int get)alue( { return ,; '

'

1)

T$pe errors

@efinite assignment rules for structs re:uire that either the struct be set to an e'isting instance of its t$pe or that each of its members be assigne to before it is reference " %n Visual C# 200!& no warning or error is pro uce when an unassigne reference t$pe member of a struct is use " 6 new warning %n Visual C# 2008& Compiler /arning 0level 11 CS10*0 is pro uce & as about the use of a shown in the following e'ample" reference t$pe member in a Cop$ Co e struct has been 2u#lic class K { 2u#lic int i;' a e "

2u#lic struct T { 2u#lic K u;' class Program {

Ravi Varma Thumati

Page 8

11/19/2013

Visual Studio.NET Best Articles

static void Main( { T t; (( Produces CS=,6,4 t.u.i & ,; ' '


%n Visual C# 200!& when $ou cast const ecimal t$pes& range checking is not alwa$s correct& an incorrect compiler errors can result" %n Visual C# 2008& the following co e pro uces the correct errorA Compiler +rror CS00,1" >ange(checking on const ecimal t$pes has been correcte " Cop$ Co e

1!

#verflow checking

static void Main( { const decimal d & P=,m; unc1ec;ed { const #yte # & (#yte d; ((CS,,G= ' '
%n Visual C# 200!& the following co e oes not pro uce a compiler error" %n Visual C# 2008& it pro uces Compiler +rror CS00,1"

1*

#verflow checking

Cop$ Co e #ut(of(boun s class Conversion conversions to { long now pro uce the correct static void Main( compiler error" {

long l+ & (long O++GGD+,G6-/5DD/-,-M; ((CS,,G=

'

'

@efinite assignment rules for unsafe pointers re:uire that the pointer be set before ereferencing the pointer" %n Visual C# 200!& when an unsafe struct contains a pointer to an arra$& accessing the pointer before assigning a value to it i not pro uce a compiler error" %n Visual C# 2008& this pro uces Compiler +rror CS01*!& as shown in the following co e" 6ccessing a fi'e ( Cop$ Co e si3e buffer in an unsafe class Test unsafe struct { before assigning a static void Main( value to the { buffer now pro uces a S* 2s; compiler error" 2sP%i[,]BB;

12

?i'e (si3e buffers

'

' (( CS,=6/

unsafe struct S { 2u#lic fi.ed int i[=,]; '


Si e effects are now @efinite preserve in assignment an null the ?? operator" coalescing e'pressions" %n Visual C# 200!& in certain scenarios& the si e effects on the left(han si e of a null coalescing e'pression are not preserve " %n such cases& the secon Console.8rite9ine statement in the following e'ample pro uces an incorrect compiler error stating that # is unassigne " %n Visual C# 2008& the same co e compiles correctl$ without an error" Cop$ Co e

18

static void Main( {

Ravi Varma Thumati

Page 9

11/19/2013

Visual Studio.NET Best Articles

int7 a@ #; a & null; Console.8rite9ine((# & null 77 =D ; (( Co error in )isual C* +,,-4 Console.8rite9ine(a B # ;
'
%n Visual C# 200!& in a try !inally construction& when control passes out of an iterator block in the try block b$ using a goto or continue statement& the !inally block is not e'ecute " %n Visual C# 2008& the !inally block is e'ecute in these cases" Cop$ Co e

using using using using

System; System.Collections.Generic; System.9in>; System.Te.t;

names2ace Qis2oseTest { class I 4 !Qis2osa#le { int mMn; internal I(int n { mMn & n; ' internal void Cot1ing( { '
The !inally block is now e'ecute when an iterator in the try block escapes with continue or goto"

15

try !inally in iterators

RI( { Console.8rite9ine(:failed to dis2ose {,':@ mMn ; ' *region !Qis2osa#le Mem#ers 2u#lic void Qis2ose( { GC.Su22ress?inaliNe(t1is ; Console.8rite9ine(:dis2ose {,':@ mMn ; ' *endregion ' class Program { static !"numera#le$I% 3( { for (int nCount & ,; nCount $ +; nCountBB { Console.8rite9ine(:loo2 start: ; using (I I & ne< I(nCount { Console.8rite9ine(:using start: ; (( Section =. (( Qis2ose not called correctly in )isual C* +,,/. if ((nCount S + && , continue;

Ravi Varma Thumati

Page 10

11/19/2013

Visual Studio.NET Best Articles

+,,/.

(( Section +. (( Qis2ose not called correctly in )isual C* yield return I; Console.8rite9ine(:using end: ; ' Console.8rite9ine(:loo2 end: ;

' static void Main(string[] args { foreac1 (I I in 3( { I.Cot1ing( ; ' Console.Head9ine( ; '
'

' yield #rea;;

'

%n Visual C# 200!& when a class oes not provi e an implementation for an interface member& the compiler substitutes base class implementations even if the$ are eclare as e'plicit interface implementations" This behavior is not in compliance with the +uropean Computer 8anufacturers 6ssociation 0+C861 specification" Visual C# 2008 correctl$ implements the specification" %n the following e'ample& Visual C# 200! prints .="Test." Visual C# 2008 correctl$ prints .6"Test. an ignores the Test metho in class = because it is an e'plicit interface implementation" Cop$ Co e

using System; interface !Test { string Test { get; ' string Test+ { get; ' ' class I 4 !Test { 2u#lic string Test { get { return :I.Test:; ' ' 2u#lic string Test+ { get { return :I.Test+:; ' ' ' class 3 4 I@ !Test { string !Test.Test { get { return :3.Test:; ' ' string !Test.Test+ { get { return :3.Test+:; ' ' ' class C 4 3@ !Test { string !Test.Test+ { get { return :C.Test+:; ' ' ' class Program {

20

Class construction now ignores e'plicit =ase classes implementations an of the same interfaces interface members in base classes"

Ravi Varma Thumati

Page 11

11/19/2013

Visual Studio.NET Best Articles

static void Main( { C c & ne< C( ; Console.8rite9ine(c.Test ; (( )isual C* +,,-4 :I.Test: '
'
Bou can mark metho s with the &bsolete attribute to cause either errors or warnings at compile time if the metho s are invoke " /hen $ou put this attribute on virtual metho s& the attribute must be put on the base metho " %f the &bsolete attribute is put on an overri e metho & it will not cause compiler errors or warnings on invocation" %n Visual C# 200!& the compiler allowe $ou to put the &bsolete attribute on an overri e metho & even though the attribute i not have an$ effect when it was put there" %n Visual C# 2008& compiler warning Compiler /arning 0level 11 CS0805 is pro uce & .#bsolete member C6"?ilenameC overri es non( obsolete member C+rror"?ilenameC". The following e'ample causes this warning" Cop$ Co e

class I 4 "rror { [System.E#soleteIttri#ute(:E#solete:@ true ] 2u#lic override string ?ilename { set { ' '
21 6ttributes 7se of an obsolete member now ' generates a compiler warning"

2u#lic static void Main( { '

2u#lic class "rror { 2u#lic virtual string ?ilename { set { ' get { return :aa:; ' ' ' class 3 { void TT( { ne< I( .?ilename & :?ilename:; ' '

Ravi Varma Thumati

Page 12

11/19/2013

Visual Studio.NET Best Articles


7se of the *pdb compiler option =uil errors without *debug now pro uces an error" %n Visual C# 200!& no warning or error is ispla$e when $ou specif$ the *pdb option but not the *debug option" Visual C# 4ust creates a >elease buil without generating the "p b file" %n the original release version of Visual C# 2008& if $ou specif$ *pdb without also specif$ing *debug& the compiler ispla$s Compiler +rror CS20,*" %n Visual C# 200!& no error is generate when a voi metho invocation is use in a switch statement" %n Visual C# 2008& Compiler +rror CS01!1 is pro uce " Cop$ Co e

22

2,

T$pe errors

6n error is now pro uce when a s#itch con ition is voi "

class C { static void Main( { (( Produces CS,=/=4 s<itc1 (M( { default4 #rea;; ' ' static void M( { '
'

2)

#verflow checking

%n Visual C# 200!& the following co e woul pro uce Compiler +rror Constant ecimal CS01,,A .The e'pression being assigne to CbC must be constant". to integral Cop$ Co e conversions now const #yte # & unc1ec;ed((#yte +/6M ; pro uce a ifferent compiler %n Visual C# 2008& Compiler +rror CS00,1 is pro uce A .Constant value C2!*8C cannot be converte to a Cb$teC". -ote that the error is pro uce error" even if the unchec"ed mo ifier is applie " %n Visual C# 2008& several issues have been correcte in which Visual C# 200! woul incorrectl$ allow operators an variables in constant e'pressions" %n Visual C# 200!& the following co e compiles without errors" %n Visual C# 2008& Compiler +rror CS01*! & Compiler /arning 0level 11 CS018) & an Compiler /arning 0level ,1 CS1218 are pro uce A Cop$ Co e

2!

Specification is more closel$ Constant a here to e'pressions regar ing constant e'pressions"

class Program { 2u#lic static int Main( { int i=@ i+@ iG@ i5@ i/; (( LasL is not 2ermitted in a constant e.2ression. if (null as o#0ect && null i= & =; (( LisL is not 2ermitted in a constant e.2ression. if (T(null is o#0ect i+ & =; (( I varia#le is not 2ermitted in a constant e.2ression. int 0G & ,; if ((, && 0G * , UU (, && , * 0G iG & =;

Ravi Varma Thumati

Page 13

11/19/2013

Visual Studio.NET Best Articles

int 05 & ,; if ((, && (05 U , i5 & =;

UU (, && (, U 05

int7 0/ & =; (( 8arning CS=D=-4 Com2arison made to same varia#le4 if (0/ && 0/ i/ & =; System.Console.8rite9ine(:{,'{='{+'{G'{5'{/':@ i=@ i+@ iG@ i5@ i/ ; ' '
%n Visual C# 200!& no error is pro uce if a static t$pe is use as a parameter to a elegate or anon$mous metho " Static t$pes cannot be use as the t$pes of metho parameters because the$ cannot be instantiate " The Visual C# 200! version of the compiler allows static t$pes as parameter t$pes within elegates an anon$mous metho eclarations" %f $ou pass null as the parameter& such elegates can be invoke " %n Visual C# 2008& error Compiler +rror CS0221 is pro uce if a static t$pe is use as a parameter to a elegate or anon$mous metho & as shown in the following e'ample" 6n error is now pro uce when a static t$pe is use as a parameter in a elegate or in a lamb a e'pression" Cop$ Co e

return =;

2*

T$pe errors

2u#lic static class Test { ' 2u#lic class Gen$T% { ' (( Produces CS,D+=4 delegate int Q(Test f ; 2u#lic class Test3 { 2u#lic static void Main( { Q d & delegate(Test f { return =; '; '
'

22

-o warning is pro uce when %n Visual C# 200!& the following co e woul pro uce Compiler /arning $ou cast a -ullable 0level ,1 CS0215" %n Visual C# 2008& no warning is pro uce " constant to a t$pes an ?? nullable t$pe e'pressions before assigning it Cop$ Co e to a nullable 0of a us1ort7 us>+ & (#yte7 ,; wi er t$pe1" #verloa resolution 6n error is now pro uce when ambiguous overloa resolution occurs on anon$mous metho s" 8etho invocations on overloa e metho s must be resolve b$ the compiler to etermine which specific overloa to invoke" /hen an invocationDs parameter t$pe is partiall$ inferre & the specific overloa to invoke can become ambiguous" This causes a compiler error" %n the case of an anon$mous metho being passe as a elegate parameter& the anon$mous metho Ds elegate t$pe is partiall$ inferre " This can lea to ambiguit$ when the compiler is selecting the correct overloa "

28

Ravi Varma Thumati

Page 14

11/19/2013

Visual Studio.NET Best Articles


%n Visual C# 200!& the compiler oes not alwa$s pro uce an error when there is no single best overloa for an anon$mous metho " %n Visual C# 2008& Compiler +rror CS0121 is pro uce & as shown in the following e'ample" Cop$ Co e

class Program { static int olMinvo;ed & ,; delegate int Q=(int . ; delegate T Q=$T%(T . ; delegate T Q=$T@ K%(K u ; static void ?(Q= d= { olMinvo;ed & =; ' static void ?$T%(Q=$T% d=t { olMinvo;ed & +; ' static void ?$T@ K%(Q=$T@ K% d=t { olMinvo;ed & G; ' static int Test,,=( { (( Produces CS,=+=4 ?(delegate(int . { return =; ' ; = return ,; else return =; ' static int Main( { return Test,,=( ; ' '
7nsafe pointers to reference t$pes are not allowe & an the$ cause compiler errors" %n Visual C# 200!& it is possible to eclare an arra$ of pointers to manage t$pes" %n Visual C# 2008& Compiler +rror CS0208 is pro uce A .Cannot take the a ress of& get the si3e of& or eclare a pointer to a manage t$pe 0CTC1". Cop$ Co e

if (olMinvo;ed &&

25

T$pe errors

6n error is now pro uce if $ou eclare an arra$ of pointers to manage t$pes"

unsafe class TestClass$T% { (( Produces CS,+,-4 static T*[] . & { '; (( Produces CS,+,-4 static void Test(T*[] arr { ' (( Produces CS,+,-4 static T*[] Test3( { return .; '
'

,0

#verloa

6 warning is now %n Visual C# 200!& when the C# compiler performs overloa resolution on

Ravi Varma Thumati

Page 15

11/19/2013

Visual Studio.NET Best Articles


generic t$pes& it oes not verif$ whether the t$pe arguments will cause the can i ate metho s to var$ b$ onl$ re! or out" 6s a result& the choice of metho s is left to the common language runtime 0C9>1 at run time& an it 4ust selects the first metho in the list" %n Visual C# 2008& Compiler /arning 09evel 11 CS15!* is pro uce when the compiler etects that two can i ate metho s for overloa resolution will var$ b$ onl$ re! or out" This con ition is illustrate in the following e'ample" Cop$ Co e

resolution

pro uce when overloa resolution can i ate metho s var$ b$ onl$ re! or out"

using System; class 3ase$T@ S% { (( Produces CS=O/64 2u#lic virtual void Test(out T . { Console.8rite9ine(:Test(out T . : ; . & default(T ; ' 2u#lic virtual void Test(ref S . { Console.8rite9ine(:Test(ref T . : ; ' ' interface !?ace { void Test(out int . ; ' class Qerived 4 3ase$int@ int%@ !?ace { static void Main( { !?ace . & ne< Qerived( ; int y; ..Test(out y ; '
'
%n Visual C# 200!& a null(coalescing e'pression with null on the left si e is evaluate as a null constant" %n Visual C# 2008& this is no longer the case" %n some cases& the Visual C# 200! behavior enables variables to be incorrectl$ treate as efinitel$ assigne " The following co e compiles an runs without error in Visual C# 200!& but in Visual C# 2008& Compiler +rror CS01*! is pro uce A .7se of unassigne local variable C'C". Cop$ Co e

,1

6 null(coalescing e'pression with -ullable null on the left t$pes an EE si e is no longer e'pressions evaluate as a null constant"

static void Main( { int .; if (null && (decimal7 (null 77 null . & =; (( Producers CS,=6/ in )isual C* +,,-4 System.Console.8rite9ine(. ; '

Ravi Varma Thumati

Page 16

11/19/2013

Вам также может понравиться