Knowledge (XXG)

C Sharp 3.0

Source đź“ť

841:
is not a plain in-memory collection, but a wrapper around a database table, it could use this technique to translate the body of the lambda into the equivalent SQL expression for optimized execution. Either way, the lambda expression itself looks exactly the same in the code, so the way it is used at
214:
class. LINQ expressions are translated to use these functions before compilation. As an alternative, which is sometimes more powerful or direct, these functions may be accessed directly. Doing so makes more use of lambda functions, which are discussed below. The following is functionally identical
932:
Developers may use extension methods to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type. In reality, extension methods are a form of syntactic sugar that provide the illusion of adding new methods to the existing class
713:
that derive directly from object. The compiler gives them a name although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object.
662:
provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. The type name is generated by the compiler and is not available at the source code level. The type of the properties is inferred by the compiler.
78:
is a new Microsoft-specific extensible, general-purpose query language for many kinds of data sources—including plain object collections, XML documents, databases, etc.—which is tightly integrated with other C# language facilities. The syntax is different from, but borrows from
933:
outside its definition. The illusion is achieved with the definition of a static method that is callable as if it were an instance method, where the receiver of the call (i.e., the instance) is bound to the first parameter of the method, decorated with keyword
832:
In the above examples, lambda expressions are merely shorthand syntax for anonymous delegates with type inference for parameters and return type. However, depending on the context they are used in, a C# compiler can also transform lambdas into
650:
for shorter local variable declarations, but it is also required for the declaration of variables of anonymous types. The contextual keyword "var", however, may only appear within a local variable declaration.
1113:
Partial methods allow code generators to generate method declarations as extension points that are only included in the compilation if someone actually implements them in another portion of a partial class.
717:
If two or more anonymous types have the same number and type of properties in the same order, the compiler treats them as the same type and they share the same compiler-generated type information.
28: 866:. There are also various new classes in that namespace that represent the expressions and partial expressions created by those methods as software objects. These include 1358: 1268: 1174: 47: 35: 879: 728:
expressions provide a concise way to write first-class anonymous function values. Compare the following C# 2.0 snippet:
725: 1194: 890:
The compiler generates a private instance variable and the appropriate accessor and mutator given code such as:
55: 51: 59: 43: 1181:
I find that I use method syntax more than query syntax because query syntax is a subset of method syntax.
834: 39: 1260: 1249: 1166: 1159: 1264: 1170: 1132: 859: 647: 1343: 1312: 1284: 1218: 710: 659: 530: 210:
To implement LINQ, a large range of new methods were added to many collections via the
58:(LINQ) pattern to the Common Language Runtime. It is not currently standardized by any 1352: 858:, or even lambda functions and other complex forms can be created dynamically using 882:
API, this can be a very powerful tool, if a little challenging to write and debug.
24: 1320: 1292: 1226: 130:// Select squares of all odd numbers in the array sorted in descending order 950:
An extension method's first parameter must take the following form, where
966:
An extension method may optionally define other parameters to follow the
322:// Result: 49, 25, 1 using 'array' as defined in previous example 862:. Much of the functionality is provided by static methods of the class 1103:// same as StringExtensions.Left(s, 3), which returns "foo"; 1133:"C# pulling ahead of Java - Lead architect paints rosy C# picture" 837:
that can then be processed at run-time. In the example above, if
1256: 75: 80: 974:
This example class demonstrates the definition and use of a
940:
The requirements for an extension method are as follows:
38:
version 3.0 was released on 19 November 2007 as part of
947:
An extension method must be defined as a static method.
944:
An extension method must be defined in a static class.
878:
and many others. When combined with aspects of the
54:, and is driven largely by the introduction of the 1248: 1158: 8: 954:is the name of the type to be extended: 1123: 842:run-time is transparent to the client. 42:. It includes new features inspired by 646:This feature is not just a convenient 16:Version of the C# programming language 19:The correct title of this article is 7: 1359:C Sharp programming language family 1344:Overview of C# 3.0 (Microsoft MSDN) 864:System.Linq.Expressions.Expression 14: 1289:.NET Framework Developer's Guide 71:LINQ (language-integrated query) 1165:. Indiana, USA: SAMS. pp.  1193:Torgersen, Mads (2006-10-10). 978:extension method for strings: 515:System.Collections.IEnumerable 1: 1313:"Partial Classes and Methods" 789:with this C# 3.0 equivalent: 525:Local variable type inference 1131:Anderson, Tim (2006-11-14). 1199:The Mellow Musings of Dr. T 1375: 23:. The substitution of the 18: 1247:Walther, Stephen (2008). 1157:Walther, Stephen (2008). 56:Language Integrated Query 34:The programming language 980: 892: 870:, which could represent 791: 730: 665: 581: 579:is interchangeable with 535: 474: 413: 374: 331: 217: 85: 1195:"What is a collection?" 410:Collection initializers 215:to the example above. 212:System.Linq.Enumerable 60:standards organisation 44:functional programming 29:technical restrictions 1251:ASP.NET 3.5 Unleashed 1161:ASP.NET 3.5 Unleashed 850:Expressions, such as 1317:C# Programming Guide 1223:C# Programming Guide 886:Automatic properties 709:Anonymous types are 205:// Result: 49, 25, 1 1079:"foo bar" 328:Object initializers 1285:"Expression Trees" 721:Lambda expressions 472:can be written as 46:languages such as 40:.NET Framework 3.5 1270:978-0-672-33011-7 1219:"Anonymous Types" 1176:978-0-672-33011-7 928:Extension methods 517:and has a public 277:OrderByDescending 1366: 1331: 1330: 1328: 1327: 1309: 1303: 1302: 1300: 1299: 1281: 1275: 1274: 1255:. Indiana, USA: 1254: 1244: 1238: 1237: 1235: 1234: 1215: 1209: 1208: 1206: 1205: 1190: 1184: 1183: 1164: 1154: 1148: 1147: 1145: 1144: 1128: 1104: 1101: 1098: 1095: 1092: 1089: 1086: 1083: 1080: 1077: 1074: 1071: 1068: 1065: 1062: 1059: 1056: 1053: 1050: 1047: 1044: 1041: 1038: 1035: 1032: 1029: 1026: 1023: 1020: 1017: 1014: 1011: 1008: 1005: 1002: 999: 996: 993: 992:StringExtensions 990: 987: 984: 977: 969: 963: 936: 923: 920: 917: 914: 911: 908: 905: 902: 899: 896: 877: 876:LambdaExpression 873: 869: 868:BinaryExpression 865: 860:expression trees 857: 853: 846:Expression trees 840: 828: 825: 822: 819: 816: 813: 810: 807: 804: 801: 798: 795: 785: 782: 779: 776: 773: 770: 767: 764: 761: 758: 755: 752: 749: 746: 743: 740: 737: 734: 705: 702: 699: 696: 693: 690: 689:"John" 687: 684: 681: 678: 675: 672: 669: 642: 639: 636: 633: 630: 627: 624: 621: 618: 615: 612: 609: 606: 603: 600: 597: 594: 591: 588: 585: 575: 572: 569: 566: 563: 560: 557: 554: 551: 548: 545: 542: 539: 520: 516: 512: 505: 502: 499: 496: 493: 490: 487: 484: 481: 478: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 426: 423: 420: 417: 405: 402: 401:"John" 399: 396: 393: 390: 387: 384: 381: 378: 368: 365: 364:"John" 362: 359: 356: 353: 350: 347: 344: 341: 338: 335: 323: 320: 317: 314: 311: 308: 305: 302: 299: 296: 293: 290: 287: 284: 281: 278: 275: 272: 269: 266: 263: 260: 257: 254: 251: 248: 245: 242: 239: 236: 233: 230: 227: 224: 221: 213: 206: 203: 200: 197: 194: 191: 188: 185: 182: 179: 176: 173: 170: 167: 164: 161: 158: 155: 152: 149: 146: 143: 140: 137: 134: 131: 128: 125: 122: 119: 116: 113: 110: 107: 104: 101: 98: 95: 92: 89: 1374: 1373: 1369: 1368: 1367: 1365: 1364: 1363: 1349: 1348: 1340: 1335: 1334: 1325: 1323: 1311: 1310: 1306: 1297: 1295: 1283: 1282: 1278: 1271: 1246: 1245: 1241: 1232: 1230: 1217: 1216: 1212: 1203: 1201: 1192: 1191: 1187: 1177: 1156: 1155: 1151: 1142: 1140: 1130: 1129: 1125: 1120: 1111: 1109:Partial methods 1106: 1105: 1102: 1099: 1096: 1093: 1090: 1087: 1084: 1081: 1078: 1075: 1072: 1069: 1066: 1063: 1060: 1057: 1054: 1051: 1048: 1045: 1042: 1039: 1036: 1033: 1030: 1027: 1024: 1021: 1018: 1015: 1012: 1009: 1006: 1003: 1000: 997: 994: 991: 988: 985: 982: 975: 967: 962: 959: 955: 953: 934: 930: 925: 924: 921: 918: 915: 912: 909: 906: 903: 900: 897: 894: 888: 875: 871: 867: 863: 855: 851: 848: 838: 830: 829: 826: 823: 820: 817: 814: 811: 808: 805: 802: 799: 796: 793: 787: 786: 783: 780: 777: 774: 771: 768: 765: 762: 759: 756: 753: 750: 747: 744: 741: 738: 735: 732: 723: 711:reference types 707: 706: 703: 701:"Doe" 700: 697: 694: 691: 688: 685: 682: 679: 676: 673: 670: 667: 660:Anonymous types 657: 655:Anonymous types 648:syntactic sugar 644: 643: 640: 637: 634: 631: 628: 625: 622: 619: 616: 613: 610: 607: 604: 601: 598: 595: 592: 589: 586: 583: 577: 576: 573: 570: 567: 564: 561: 558: 555: 552: 549: 546: 543: 540: 537: 529:Local variable 527: 518: 514: 510: 507: 506: 503: 500: 497: 494: 491: 488: 485: 482: 479: 476: 470: 469: 466: 463: 460: 457: 454: 451: 448: 445: 442: 439: 436: 433: 430: 427: 424: 421: 418: 415: 412: 407: 406: 403: 400: 397: 394: 391: 388: 385: 382: 379: 376: 372:can be written 370: 369: 366: 363: 360: 357: 354: 351: 348: 345: 342: 339: 336: 333: 330: 325: 324: 321: 318: 315: 312: 309: 306: 303: 300: 297: 294: 291: 288: 285: 282: 279: 276: 273: 270: 267: 264: 261: 258: 255: 252: 249: 246: 243: 240: 237: 234: 231: 228: 225: 222: 219: 211: 208: 207: 204: 201: 198: 195: 192: 189: 186: 183: 180: 177: 174: 171: 168: 165: 162: 159: 156: 153: 150: 147: 144: 141: 138: 135: 132: 129: 126: 123: 120: 117: 114: 111: 108: 105: 102: 99: 96: 93: 90: 87: 73: 68: 66:C# 3.0 features 32: 17: 12: 11: 5: 1372: 1370: 1362: 1361: 1351: 1350: 1347: 1346: 1339: 1338:External links 1336: 1333: 1332: 1304: 1276: 1269: 1239: 1210: 1185: 1175: 1149: 1139:. The Register 1122: 1121: 1119: 1116: 1110: 1107: 981: 972: 971: 964: 960: 957: 951: 948: 945: 929: 926: 893: 887: 884: 847: 844: 792: 731: 722: 719: 666: 656: 653: 582: 536: 531:type inference 526: 523: 509:assuming that 475: 414: 411: 408: 375: 332: 329: 326: 218: 86: 83:. An example: 72: 69: 67: 64: 15: 13: 10: 9: 6: 4: 3: 2: 1371: 1360: 1357: 1356: 1354: 1345: 1342: 1341: 1337: 1322: 1318: 1314: 1308: 1305: 1294: 1290: 1286: 1280: 1277: 1272: 1266: 1262: 1258: 1253: 1252: 1243: 1240: 1228: 1224: 1220: 1214: 1211: 1200: 1196: 1189: 1186: 1182: 1178: 1172: 1168: 1163: 1162: 1153: 1150: 1138: 1137:Reg Developer 1134: 1127: 1124: 1117: 1115: 1108: 979: 965: 961:parameterName 949: 946: 943: 942: 941: 938: 927: 891: 885: 883: 881: 861: 845: 843: 836: 790: 729: 727: 720: 718: 715: 712: 664: 661: 654: 652: 649: 580: 534: 532: 524: 522: 473: 409: 373: 327: 216: 84: 82: 77: 70: 65: 63: 61: 57: 53: 49: 45: 41: 37: 30: 26: 22: 1324:. Retrieved 1316: 1307: 1296:. Retrieved 1288: 1279: 1250: 1242: 1231:. Retrieved 1222: 1213: 1202:. Retrieved 1198: 1188: 1180: 1160: 1152: 1141:. Retrieved 1136: 1126: 1112: 973: 939: 931: 889: 849: 831: 788: 724: 716: 708: 658: 645: 578: 528: 508: 471: 371: 209: 74: 33: 20: 1259:. pp.  1229:. July 2008 513:implements 220:IEnumerable 133:IEnumerable 1326:2009-04-28 1298:2009-04-26 1233:2009-06-18 1204:2009-06-18 1143:2007-01-20 1118:References 970:parameter. 880:reflection 617:Dictionary 584:Dictionary 550:Dictionary 187:descending 27:is due to 1321:Microsoft 1293:Microsoft 1227:Microsoft 1046:Substring 872:x <= y 856:a = b + c 852:x <= y 839:listOfFoo 794:listOfFoo 733:listOfFoo 683:FirstName 1353:Category 745:delegate 695:LastName 638:>> 605:>> 571:>> 521:method. 389:Customer 377:Customer 346:Customer 334:Customer 1261:950–952 1167:916–917 913:private 181:orderby 48:Haskell 1267:  1173:  1070:string 1037:return 1016:string 1004:string 1001:static 998:public 986:static 983:public 898:string 895:public 763:return 726:Lambda 623:string 590:string 556:string 511:MyList 489:MyList 477:MyList 428:MyList 416:MyList 298:Select 190:select 21:C# 3.0 989:class 956:this 809:=> 800:Where 739:Where 635:float 602:float 568:float 307:=> 286:=> 253:=> 244:Where 238:array 232:query 163:where 160:array 145:query 91:array 1265:ISBN 1257:SAMS 1171:ISBN 1091:Left 1013:this 1007:Left 976:Left 968:this 958:type 952:type 935:this 901:Name 835:ASTs 821:> 818:Size 775:> 772:Size 632:< 629:List 620:< 599:< 596:List 587:< 565:< 562:List 553:< 480:list 452:list 434:list 419:list 395:Name 358:Name 229:> 223:< 151:from 142:> 136:< 76:LINQ 50:and 1025:int 916:set 907:get 784:}); 751:Foo 677:new 668:var 641:(); 614:new 574:(); 547:new 538:var 519:Add 486:new 458:Add 440:Add 431:(); 425:new 386:new 349:(); 343:new 226:int 139:int 88:int 81:SQL 1355:: 1319:. 1315:. 1291:. 1287:. 1263:. 1225:. 1221:. 1197:. 1179:. 1169:. 1135:. 1100:); 1061:); 937:. 874:; 854:, 827:); 824:10 778:10 704:}; 533:: 504:}; 467:); 449:); 404:}; 319:); 265:== 175:== 157:in 127:}; 118:10 62:. 52:ML 36:C# 1329:. 1301:. 1273:. 1236:. 1207:. 1146:. 1097:3 1094:( 1088:. 1085:s 1082:; 1076:= 1073:s 1067:} 1064:} 1058:n 1055:, 1052:0 1049:( 1043:. 1040:s 1034:{ 1031:) 1028:n 1022:, 1019:s 1010:( 995:{ 922:} 919:; 910:; 904:{ 815:. 812:x 806:x 803:( 797:. 781:; 769:. 766:x 760:{ 757:) 754:x 748:( 742:( 736:. 698:= 692:, 686:= 680:{ 674:= 671:x 626:, 611:= 608:x 593:, 559:, 544:= 541:x 501:2 498:, 495:1 492:{ 483:= 464:2 461:( 455:. 446:1 443:( 437:. 422:= 398:= 392:{ 383:= 380:c 367:; 361:= 355:. 352:c 340:= 337:c 316:x 313:* 310:x 304:x 301:( 295:. 292:) 289:x 283:x 280:( 274:. 271:) 268:1 262:2 259:% 256:x 250:x 247:( 241:. 235:= 202:; 199:x 196:* 193:x 184:x 178:1 172:2 169:% 166:x 154:x 148:= 124:7 121:, 115:, 112:2 109:, 106:5 103:, 100:1 97:{ 94:= 31:. 25:#

Index

#
technical restrictions
C#
.NET Framework 3.5
functional programming
Haskell
ML
Language Integrated Query
standards organisation
LINQ
SQL
type inference
syntactic sugar
Anonymous types
reference types
Lambda
ASTs
expression trees
reflection
"C# pulling ahead of Java - Lead architect paints rosy C# picture"
ASP.NET 3.5 Unleashed
916–917
ISBN
978-0-672-33011-7
"What is a collection?"
"Anonymous Types"
Microsoft
ASP.NET 3.5 Unleashed
SAMS
950–952

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.

↑