Visual C# 2008 Breaking Changes

download Visual C# 2008 Breaking Changes

of 16

Transcript of Visual C# 2008 Breaking Changes

  • 8/13/2019 Visual C# 2008 Breaking Changes

    1/16

    Visual Studio.NETBest Articles

    Visual C# 2008 Breaking ChangesThe following table lists all the breaking changes in Visual C# 2008 Service Pack 1 that might impact an applicationthat was create in the original release version of Visual C# 2008 or in Visual C# 200!"

    Changenumber Category Issue Description

    1 verloaresolution

    T$pe inference is nowinclu e on arra$s ofpointer t$pes inmetho overloaresolution"

    %n Visual C# 2008 an earlier& t$pe inference causes arra$s ofpointer t$pes to be e'clu e from the metho overloa resolutionprocess" %n the following co e& the Visual C# 200! compiler selectsthe non(generic version of Test because the generic version of Test is e'clu e from consi eration because of its t$pe parameter ofint*[] " %n Visual C# 2008& the generic version of Test is selecte "

    Cop$ Co eusing System.Collections.Generic;

    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 { ''

    2 %n e'ers

    Compiler nowpro uces errorCS0)** for in e'ersan properties ina ition to metho s"

    %n the original release version of Visual C# 2008 an earlierversions& it is possible to efine an e'plicit implementation of an

    in e'er in which the implementation has a params parameter butthe interface efinition oes not" This construction is contrar$ to the

    specification" %n Visual C# 2008 SP1& this construction pro ucesCompiler +rror CS0)** & as shown in the following co e"

    Cop$ Co einterface !{ int t1is[int[] 2] { set; ''

    class 3ase 4 !{ Produces CS,5664

    int !.t1is[2arams int[] 2] { set { ' '

    '

    , -ullablet$pes an ?? e'pressions

    Compiler nowcorrectl$ evaluatese'pressions in whichnullable variables arecompare tothemselves"

    %n the original release version of Visual C# 2008& the following co ecompiles an outputs .false. at run time" %n Visual C# 2008 ServicePack 1& Compiler /arning level , CS1 18 is pro uce an .true. isoutput"

    Cop$ Co e

    Ravi Varma Thumati Page 1 11/19/2013

    http://msdn.microsoft.com/en-us/library/ms228620.aspxhttp://msdn.microsoft.com/en-us/library/78kc05h3.aspxhttp://msdn.microsoft.com/en-us/library/ms228620.aspxhttp://msdn.microsoft.com/en-us/library/78kc05h3.aspx
  • 8/13/2019 Visual C# 2008 Breaking Changes

    2/16

    Visual Studio.NETBest Articles

    static class Program{ static void Main( { int7 & null; #ool y & && ; Console.8rite9ine(y ;

    '') try !inally in

    iterators+'ecution of neste!inally blocks fromiterators that havebrea" statements ischange "

    %n the original release version of Visual C# 2008& the following co ee'ecutes the outer !inally two times" %n Visual C# 2008 SP1& theouter !inally is e'ecute one time"

    Cop$ Co eusing 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: ;

  • 8/13/2019 Visual C# 2008 Breaking Changes

    3/16

    Visual Studio.NETBest Articles

    ' ' ''

    ! +'pressiontrees

    %ncorrect bo'ing ofmetho e'pressions ine'pression trees nolonger occurs"

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

    Cop$ Co eusing System;using System.9in>;using System.9in>." 2ressions;

    class Program{ static void Main( { Test$S%( ; '

    static void Test$T%(

  • 8/13/2019 Visual C# 2008 Breaking Changes

    4/16

    Visual Studio.NETBest Articles

    static void Main( { Test$S%( ; '

    static void Test$T%(

  • 8/13/2019 Visual C# 2008 Breaking Changes

    5/16

    Visual Studio.NETBest Articles

    C# 2008 SP1& a System.ArgumentE$ception is thrown& as shownin the following co e"

    Cop$ Co eusing 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

  • 8/13/2019 Visual C# 2008 Breaking Changes

    6/16

  • 8/13/2019 Visual C# 2008 Breaking Changes

    7/16

    Visual Studio.NETBest Articles

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

    select i;

    )isual C +,,- SP= t1ro

  • 8/13/2019 Visual C# 2008 Breaking Changes

    8/16

  • 8/13/2019 Visual C# 2008 Breaking Changes

    9/16

    Visual Studio.NETBest Articles

    static void Main( { T t; Produces CS=,6,4

    t.u.i & ,;'

    '

    1! verflowchecking

    >ange(checkingon const ecimalt$pes has beencorrecte "

    %n Visual C# 200!& when $ou cast const ecimal t$pes& range checking isnot alwa$s correct& an incorrect compiler errors can result" %n Visual C#2008& the following co e pro uces the correct errorA Compiler +rrorCS00,1 "

    Cop$ Co e static void Main( { const decimal d & =,m; unc1ec ed { const #yte # & (#yte d; CS,, = ' '

    1* verflowchecking

    ut(of(boun sconversions tolong now pro ucethe correctcompiler error"

    %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 "

    Cop$ Co eclass Conversion

    { static void Main(

    { long l+ & (long O++ D+, 6-/5DD/-,-M; CS,, =

    ' '

    1 ?i'e (si3ebuffers

    6ccessing a fi'e (si3e buffer in anunsafe structbefore assigning avalue to thebuffer nowpro uces acompiler error"

    @efinite assignment rules for unsafe pointers re:uire that the pointer beset before ereferencing the pointer" %n Visual C# 200!& when an unsafestruct contains a pointer to an arra$& accessing the pointer beforeassigning 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 followingco e"

    Cop$ Co e unsafe class Test { static void Main( { S* 2s; 2s %i[,]BB; ' CS,=6/ '

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

    18

    Si e effectsare nowpreserve innullcoalescinge'pressions"

    @efiniteassignment anthe ?? operator"

    %n Visual C# 200!& in certain scenarios& the si e effects on the left(hansi e of a null coalescing e'pression are not preserve " %n such cases& thesecon Console.8rite9ine statement in the following e'ample pro ucesan incorrect compiler error stating that # is unassigne " %n Visual C#2008& the same co e compiles correctl$ without an error"

    Cop$ Co e static void Main( {

    Ravi Varma Thumati Page 9 11/19/2013

    http://msdn.microsoft.com/en-us/library/e6w8fe1b.aspxhttp://msdn.microsoft.com/en-us/library/879y0b2y.aspxhttp://msdn.microsoft.com/en-us/library/879y0b2y.aspxhttp://msdn.microsoft.com/en-us/library/879y0b2y.aspxhttp://msdn.microsoft.com/en-us/library/4y7h161d.aspxhttp://msdn.microsoft.com/en-us/library/4y7h161d.aspxhttp://msdn.microsoft.com/en-us/library/e6w8fe1b.aspxhttp://msdn.microsoft.com/en-us/library/879y0b2y.aspxhttp://msdn.microsoft.com/en-us/library/879y0b2y.aspxhttp://msdn.microsoft.com/en-us/library/879y0b2y.aspxhttp://msdn.microsoft.com/en-us/library/4y7h161d.aspx
  • 8/13/2019 Visual C# 2008 Breaking Changes

    10/16

    Visual Studio.NETBest Articles

    int7 a@ #; a & null; Console.8rite9ine((# & null 77 =D ; o error in )isual C +,,-4

    Console.8rite9ine(a B # ;'

    15 try !inally in iterators

    The !inally blockis now e'ecutewhen an iteratorin the try blockescapes withcontinue or

    goto "

    %n Visual C# 200!& in a try !inally construction& when control passes outof 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 eusing System;using System.Collections.Generic;using System.9in>;using System.Te t;

    names2ace Qis2oseTest{ class I 4 !Qis2osa#le { int m n; internal I(int n { m n & n; ' internal void ot1ing( { '

    RI( { Console.8rite9ine(:failed to dis2ose {,':@ m n ; ' region !Qis2osa#le Mem#ers

    2u#lic void Qis2ose( { GC.Su22ress?inaliNe(t1is ; Console.8rite9ine(:dis2ose {,':@ m n ; '

    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 + && , continue;

    Ravi Varma Thumati Page 10 11/19/2013

  • 8/13/2019 Visual C# 2008 Breaking Changes

    11/16

    Visual Studio.NETBest Articles

    Section +. Qis2ose not called correctly in )isual C+,,/. yield return I;

    Console.8rite9ine(:using end: ;

    ' Console.8rite9ine(:loo2 end: ; ' yield #rea ; ' static void Main(string[] args { foreac1 (I I in 3( { I. ot1ing( ; ' Console.Head9ine( ; '

    ''

    20=ase classesaninterfaces

    Class constructionnow ignorese'plicitimplementationsof the sameinterfacemembers in baseclasses"

    %n Visual C# 200!& when a class oes not provi e an implementation foran interface member& the compiler substitutes base class implementationseven if the$ are eclare as e'plicit interface implementations" Thisbehavior is not in compliance with the +uropean Computer anufacturers6ssociation +C 6 specification" Visual C# 2008 correctl$ implements thespecification" %n the following e'ample& Visual C# 200! prints .="Test."Visual C# 2008 correctl$ prints .6"Test. an ignores the Test metho inclass = because it is an e'plicit interface implementation"

    Cop$ Co eusing 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{

    Ravi Varma Thumati Page 11 11/19/2013

  • 8/13/2019 Visual C# 2008 Breaking Changes

    12/16

    Visual Studio.NETBest Articles

    static void Main( { C c & ne< C( ; Console.8rite9ine(c.Test ; )isual C +,,-4 :I.Test:

    ''

    21 6ttributes

    7se of an obsoletemember nowgenerates acompiler warning"

    Bou can mark metho s with the &bsolete attribute to cause either errorsor warnings at compile time if the metho s are invoke " /hen $ou putthis attribute on virtual metho s& the attribute must be put on the basemetho " %f the &bsolete attribute is put on an overri e metho & it will notcause compiler errors or warnings on invocation" %n Visual C# 200!& thecompiler allowe $ou to put the &bsolete attribute on an overri emetho & even though the attribute i not have an$ effect when it wasput there" %n Visual C# 2008& compiler warning Compiler /arning level1 CS0805 is pro uce & . bsolete member 6"?ilename overri es non(obsolete member +rror"?ilename ". The following e'ample causes thiswarning"

    Cop$ Co eclass I 4 "rror{ [System.E#soleteIttri#ute(:E#solete:@ true ] 2u#lic override string ?ilename { set { ' '

    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

    http://msdn.microsoft.com/en-us/library/bb397626.aspxhttp://msdn.microsoft.com/en-us/library/bb397626.aspxhttp://msdn.microsoft.com/en-us/library/bb397626.aspxhttp://msdn.microsoft.com/en-us/library/bb397626.aspxhttp://msdn.microsoft.com/en-us/library/bb397626.aspx
  • 8/13/2019 Visual C# 2008 Breaking Changes

    13/16

  • 8/13/2019 Visual C# 2008 Breaking Changes

    14/16

    Visual Studio.NETBest Articles

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

    int7 0/ & =; 8arning CS=D=-4 Com2arison made to same varia#le4

    if (0/ && 0/

    i/ & =;

    System.Console.8rite9ine(:{,'{='{+'{ '{5'{/':@i=@ i+@ i @ i5@ i/ ;

    return =; '

    '

    2* T$pe errors

    6n error is nowpro uce when astatic t$pe is useas a parameter ina elegate or in alamb ae'pression"

    %n Visual C# 200!& no error is pro uce if a static t$pe is use as aparameter to a elegate or anon$mous metho " Static t$pes cannot beuse as the t$pes of metho parameters because the$ cannot beinstantiate " The Visual C# 200! version of the compiler allows statict$pes as parameter t$pes within elegates an anon$mous methoeclarations" %f $ou pass null as the parameter& such elegates can beinvoke " %n Visual C# 2008& error Compiler +rror CS0 21 is pro uce if astatic t$pe is use as a parameter to a elegate or anon$mous metho & asshown in the following e'ample"

    Cop$ Co e2u#lic static class Test { '2u#lic class Gen$T% { '

    Produces CS,D+=4delegate int Q(Test f ;

    2u#lic class Test3{ 2u#lic static void Main( { Q d & delegate(Test f { return =; '; ''

    2-ullablet$pes an ??e'pressions

    -o warning ispro uce when$ou cast aconstant to anullable t$pebefore assigning itto a nullable of awi er t$pe "

    %n Visual C# 200!& the following co e woul pro uce Compiler /arninglevel , CS0215 " %n Visual C# 2008& no warning is pro uce "

    Cop$ Co eus1ort7 us>+ & (#yte7 ,;

    28 verloaresolution

    6n error is nowpro uce whenambiguousoverloaresolution occurson anon$mousmetho s"

    etho invocations on overloa e metho s must be resolve b$ thecompiler to etermine which specific overloa to invoke" /hen aninvocationDs parameter t$pe is partiall$ inferre & the specific overloa toinvoke can become ambiguous" This causes a compiler error"

    %n the case of an anon$mous metho being passe as a elegateparameter& the anon$mous metho Ds elegate t$pe is partiall$ inferre "This can lea to ambiguit$ when the compiler is selecting the correctoverloa "

    Ravi Varma Thumati Page 14 11/19/2013

    http://msdn.microsoft.com/en-us/library/td8fh082.aspxhttp://msdn.microsoft.com/en-us/library/td8fh082.aspxhttp://msdn.microsoft.com/en-us/library/4kycwe2x.aspxhttp://msdn.microsoft.com/en-us/library/4kycwe2x.aspxhttp://msdn.microsoft.com/en-us/library/td8fh082.aspxhttp://msdn.microsoft.com/en-us/library/4kycwe2x.aspxhttp://msdn.microsoft.com/en-us/library/4kycwe2x.aspx
  • 8/13/2019 Visual C# 2008 Breaking Changes

    15/16

    Visual Studio.NETBest Articles

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

    Cop$ Co eclass Program{

    static int ol invo ed & ,;

    delegate int Q=(int ; delegate T Q=$T%(T ; delegate T Q=$T@ K%(K u ;

    static void ?(Q= d= { ol invo ed & =; ' static void ?$T%(Q=$T% d=t { ol invo ed & +; ' static void ?$T@ K%(Q=$T@ K% d=t { ol invo ed & ; '

    static int Test,,=( {

    Produces CS,=+=4 ?(delegate(int { return =; ' ; if (ol invo ed &&= return ,; else return =; '

    static int Main( { return Test,,=( ; ''

    25 T$pe errors

    6n error is nowpro uce if $oueclare an arra$of pointers tomanage t$pes"

    7nsafe pointers to reference t$pes are not allowe & an the$ causecompiler errors" %n Visual C# 200!& it is possible to eclare an arra$ ofpointers to manage t$pes" %n Visual C# 2008& Compiler +rror CS0208 ispro uce A .Cannot take the a ress of& get the si3e of& or eclare apointer to a manage t$pe T ".

    Cop$ Co eunsafe 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

    http://msdn.microsoft.com/en-us/library/ky5f1hz8.aspxhttp://msdn.microsoft.com/en-us/library/ky5f1hz8.aspxhttp://msdn.microsoft.com/en-us/library/x2estayf.aspxhttp://msdn.microsoft.com/en-us/library/x2estayf.aspxhttp://msdn.microsoft.com/en-us/library/ky5f1hz8.aspxhttp://msdn.microsoft.com/en-us/library/x2estayf.aspx
  • 8/13/2019 Visual C# 2008 Breaking Changes

    16/16

    Visual Studio.NETBest Articles

    resolution

    pro uce whenoverloaresolutioncan i atemetho s var$ b$onl$ re! or out "

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

    Cop$ Co e

    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 ; ''

    ,1-ullablet$pes an EEe'pressions

    6 null(coalescinge'pression withnull on the leftsi e is no longerevaluate as anull constant"

    %n Visual C# 200!& a null(coalescing e'pression with null on the left si e isevaluate as a null constant" %n Visual C# 2008& this is no longer thecase" %n some cases& the Visual C# 200! behavior enables variables to beincorrectl$ treate as efinitel$ assigne " The following co e compiles anruns without error in Visual C# 200!& but in Visual C# 2008& Compiler+rror CS01*! is pro uce A .7se of unassigne local variable ' ".

    Cop$ Co estatic 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

    http://msdn.microsoft.com/en-us/library/bb882526.aspxhttp://msdn.microsoft.com/en-us/library/bb882526.aspxhttp://msdn.microsoft.com/en-us/library/bb882526.aspxhttp://msdn.microsoft.com/en-us/library/4y7h161d.aspxhttp://msdn.microsoft.com/en-us/library/4y7h161d.aspxhttp://msdn.microsoft.com/en-us/library/bb882526.aspxhttp://msdn.microsoft.com/en-us/library/bb882526.aspxhttp://msdn.microsoft.com/en-us/library/4y7h161d.aspxhttp://msdn.microsoft.com/en-us/library/4y7h161d.aspx