www.it-ebooks.info www.it-ebooks.info C# 5.0 PROGRAMMER’S REFERENCE Introduction xxxiii ▶▶ Part I The C# Ecosystem Chapter The C# Environment Chapter Writing a First Program 11 Chapter Program and Code File Structure 27 ▶▶ Part II C# Language Elements Chapter Data Types, Variables, and Constants 53 Chapter Operators 99 Chapter Methods 121 Chapter Program Control Statements 151 Chapter LINQ 169 Chapter Error Handling 205 Chapter 10 Tracing and Debugging 231 ▶▶ Part III Object-Oriented Programming Chapter 11 OOP Concepts 245 Chapter 12 Classes and Structures 269 Chapter 13 Namespaces 303 Chapter 14 Collection Classes 317 Chapter 15 Generics 343 ▶▶ Part IV Interacting with the Environment Chapter 16 Printing 359 Chapter 17 Configuration and Resources 393 Chapter 18 Streams 411 Chapter 19 File System Objects 425 Chapter 20 Networking 445 Continues www.it-ebooks.info ▶▶ Part V Advanced Topics Chapter 21 Regular Expressions 469 Chapter 22 Parallel Programming 485 Chapter 23 ADO.NET 509 Chapter 24 XML 533 Chapter 25 Serialization 563 Chapter 26 Reflection 581 Chapter 27 Cryptography 601 ▶▶ Part VI Appendices Appendix A Solutions to Exercises 625 Appendix B Data Types 733 Appendix C Variable Declarations 737 Appendix D Constant Declarations 741 Appendix E Operators 743 Appendix F Method Declarations 749 Appendix G Useful Attributes 753 Appendix H Control Statements 757 Appendix I Error Handling 761 Appendix J LINQ 763 Appendix K Classes and Structures 773 Appendix L Collection Classes 777 Appendix M Generic Declarations 783 Appendix N Printing and Graphics 785 Appendix O Useful Exception Classes 799 Appendix P Date and Time Format Specifiers 803 Appendix Q Other Format Specifiers 807 Appendix R Streams 813 Appendix S Filesystem Classes 821 Appendix T Regular Expressions 835 Appendix U Parallel Programming 843 www.it-ebooks.info Appendix V XML 849 Appendix W Serialization 859 Appendix X Reflection 865 Index 877 www.it-ebooks.info www.it-ebooks.info C# 5.0 Programmer’s Reference www.it-ebooks.info www.it-ebooks.info C# 5.0 Programmer’s Reference Rod Stephens www.it-ebooks.info C# 5.0 Programmer’s Reference Published by John Wiley & Sons, Inc 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2014 by John Wiley & Sons, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-1-118-84728-2 ISBN: 978-1-118-84697-1 (ebk) ISBN: 978-1-118-84729-9 (ebk) Manufactured in the United States of America 10 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600 Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 7486008, or online at http://www.wiley.com/go/permissions Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose No warranty may be created or extended by sales or promotional materials The advice and strategies contained herein may not be suitable for every situation This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services If professional assistance is required, the services of a competent professional person should be sought Neither the publisher nor the author shall be liable for damages arising herefrom The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002 Wiley publishes in a variety of print and electronic formats and by print-on-demand Some material included with standard print versions of this book may not be included in e-books or in print-on-demand If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com For more information about Wiley products, visit www.wiley.com Library of Congress Control Number: 2014930410 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc and/or its affiliates, in the United States and other countries, and may not be used without written permission All other trademarks are the property of their respective owners John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book www.it-ebooks.info preprocessor directives – properties, EventInfo class preprocessor directives See also directives #define, 31–33 #elif, 33–34 #else, 33–34 #endif, 33–34 #endregion, 35–36 #error, 34 #if, 33–34 #line, 34–35 #pragma, 36–37 #region, 35–36 #undef, 31–33 #warning, 34 previewing printing, 381 PrintDialog object, 382 PrintDocument class, 360 BeginPrint event, 360 EndPrint event, 361 page settings, 364 PrintPage event, 360 QueryPageSettings event, 360 regeneration, 362 SmoothingMode, 364 printing booklet example, 373–380 images, 372–373 page settings, 364 previews, 381 Windows Forms, 359–360, 785–786 drawing graphics, 787–795 graphics namespaces, 786–787 PrintDocument class, 360 WPF, 380 document creation, 796–797 paginator, 381–385, 795–796 PrintPreviewControl, 361–362 PrintPreviewDialog control, 361 private keyword, 271 private properties, 249 procedures, 121 processors, CPUs, 485 Program class, 14 program control break statements, 165 conditional operators, 157 continue statement, 165–166 decision (control) statements, 151 if-else, 152–153 switch, 153–156 enumerated values, 156–157 enumerators, 163–164 iterators, 164 looping statements loops, 160 foreach loops, 160–163 for loops, 157–160 while loops, 160 null-coalescing operators, 157 projects console, 12 New Project dialog, 12 solutions, 27 templates, using directive, 307–309 Windows Forms, 12 Windows Store, 12 WPF, 12 properties, 246–247 accessor methods, 86–88 ArrayList class, 321–324 DefaultProperty, 123, 755 DriveInfo class AvailableFreeSpace, 825 DriveFormat, 825 DriveType, 825 IsReady, 825 Name, 825 RootDirectory, 825 TotalFreespace, 825 TotalSize, 825 VolumeLabel, 825 EventInfo class AddMethod, 870 Attributes, 870 EventHandlerType, 870 904 www.it-ebooks.info properties, FieldInfo class – properties, Stream class RaiseMethod, 870 IsGenericMethod, 872 RemoveMethod, 870 IsGenericMethodDefinition, 872 FieldInfo class IsPrivate, 872 Attributes, 873 IsPublic, 872 FieldType, 873 IsStatic, 872 IsAssembly, 873 IsVirtual, 872 IsFamily, 873 ReturnParameter, 872 IsFamilyAndAssembly, 873 ReturnType, 872 IsFamilyOrAssembly, 873 ParameterInfo class IsInitOnly, 873 Attributes, 874 IsLiteral, 873 CustomAttributes, 874 IsNotSerialized, 873 DefaultValue, 875 IsPrivate, 873 HasDefaultValue, 875 IsPublic, 873 IsIn, 875 IsStatic, 873 IsOptional, 875 FileSystemWatcher class IsOut, 875 EnableRaisingEvents, 829 Member, 875 Filter, 829 Name, 875 IncludeSubdirectories, 829 ParameterType, 875 InternalBufferSize, 829 Position, 875 NotifyFilter, 830 ParenthesizePropertyName, 123, 756 InvokeRequired, 488–491 Path class Language, 407 AltDirectorySeparatorChar, 830 Localizable, 407 DirectorySeparatorChar, 830 MemberInfo class InvalidPathChars, 831 CustomAttributes, 870 PathSeparator, 831 DeclaringType, 870 VolumeSeparatorChar, 831 MemberTypes, 870 private, 249 Module, 870 PropertyInfo class, 874 Name, 870 Attributes, 874 ReflectedType, 870 CanRead, 874 MergableProperty, 123 CanWrite, 874 MethodInfo class GetMethod, 874 ContainsGenericParameters, 871 IsAbstract, 871 IsAssembly, 871 IsConstructor, 871 IsFamily, 871 IsFamilyAndAssembly, 871 IsFamilyOrAssembly, 871 IsFinal, 871 PropertyType, 874 SetMethod, 874 reflection getting, 585–589 setting, 585–589 Stream class, 413 CanRead, 814 CanSeek, 814 905 www.it-ebooks.info properties, Stream class (continued) – PropertyInfo class IsVisible, 867 properties (continued) CanTimeout, 814 Module, 867 CanWrite, 814 Name, 867 Length, 814 Namespace, 867 Position, 814 StructLayoutAttribute, 867 ReadTimeout, 814 TypeInitializer, 867 WriteTimeout, 814 XElement class Type class Document, 851 Assembly, 865 FirstAttribute, 851 Attributes, 865 FirstNode, 851 BaseType, 865 HasAttributes, 851 ContainsGenericParameters, 865 HasElements, 851 CustomAttributes, 865 IsEmpty, 852 FullName, 865 LastAttribute, 852 GetProperty method, 869 LastNode, 852 IsAbstract, 865 Name, 852 IsArray, 865 NextNode, 852 IsAutoLayout, 865 NodeType, 852 IsByRef, 866 Parent, 852 IsClass, 866 PreviousNode, 852 IsCOMObject, 866 IsEnum, 866 Value, 852 XmlReader class IsExplicitLayout, 866 AttributeCount, 855 IsGenericType, 866 Depth, 855 IsImport, 866 EOF, 855 IsInterface, 866 HasAttributes, 855 IsLayoutSequential, 866 HasValue, 855 IsMarshalByRef, 866 IsEmptyElement, 855 IsNested, 866 Item, 855 IsNestedAssembly, 866 Name, 855 IsNestedFamANDAssem, 866 IsNestedFamily, 866 IsNestedFamORAssem, 866 IsNestedPrivate, 866 IsNestedPublic, 866 IsNotPublic, 866 IsPointer, 866 IsPrimitive, 866 IsPublic, 866 IsSealed, 866 IsSerializable, 866 IsValueType, 867 Value, 855 Properties folder, 28 property procedures, methods, 750 PropertyInfo class, 874 Attributes property, 874 CanRead property, 874 CanWrite property, 874 GetAccessors method, 874 GetGetMethod method, 874 GetIndexParameters method, 874 GetMethod property, 874 GetSetMethod method, 874 906 www.it-ebooks.info protected internal keyword – #region directive GetValue method, 874 Shell32.Shell interface, 440–443, 833 PropertyType property, 874 SHEmptyRecycleBin API function, 439–440 SetMethod property, 874 SetValue method, 874 SHEmptyRecyclebin function, 833 protected internal keyword, 271 protected keyword, 271 public, 271 Push method, 332 Q quantifiers in regular expressions, 475 queries, LINQ lambda functions, 187–188 method-based, 185–187 Queue class, 336, 779 methods, 333–334 properties, 333–334 queues, 333–335 QuickWatch command (Debug menu), 233 R race conditions, 499–501 raising events, 292 parent class, 296–297 random number generation, 603 randomness in cryptography encryption for random numbers, 604 random number generation, 605–608 random numbers for encryption, 604 secure randomness, 604–605 Range function, 767 readonly, variable declaration and, 61 ReadOnly attribute, 123, 756 readonly keyword, 64–65 RecommendedAsConfigurable attribute, 123 RecordMessage method, 272–273 recycle bin, 438 DeleteDictionary method, 438–439 FileSystem class, 833 ref keyword, 84, 278–279 reference equality, 113–115 reference type variables, 57 reference types, 275 references parameter declaration, 83–84 Solution Explorer, 30 refinement, 252–256 reflection, 581 assemblies, 589–591 classes, 582–585 EventInfo class, 870–871 FieldInfo class, 873 MemberInfo class, 870 MethodInfo class, 871–872 methods, invoking, 591–593 ParameterInfo class, 874–875 properties getting, 585–589 setting, 585–589 PropertyInfo class, 874 scripts, running, 593–596 Type class, 865–869 RefreshProperties attribute, 123 Regex class, 839–840 finding matches, 840–841 IsMatch method, 479–480, 840 Match method, 841 Matches collection, 482–483 Matches method, 480–481, 841 methods, 479 pattern matching, 840 RegexOptions parameter, 474 Replace method, 481–482 replacements, 841 RegexOptions parameter, 474 #region directive, 35–36 907 www.it-ebooks.info registry – serialization registry configuration information, 398 hives, 397 keys, 397–401 uninstallation and, 402 Registry class, 398 hive fields, 398–399 RegistryKey class, 398, 400–401 methods, 399 properties, 399 RegistryTools class, 401–402 regular expressions, 469–470 alternation constructs, 476 anchors, 473–474, 836–837 building, 470–478 capture groups, 474–475 character classes, 472–473, 836 escape sequences, 470–471 character escapes, 471–472, 835 finding matches, 480–481 grouping constructs, 474–475 options, 837 alternation constructs, 838 grouping constraints, 838 quantifiers, 838 parsing input, 482–483 pattern matching, 479–480 quantifers, 475 Regex class, 839–840 finding matches, 840–841 pattern matching, 840 replacements, 841 replacements, 481–482 samples, 476–478, 839 testing, 478 RegularExpressions namespace, 470 relational databases, 510 Repeat function, 767 Replace method, 481–482 Requires method, 209–211 Reset method, 760 Resize method, 778 resource files application resources, 405–406 embedded resources, 406 localization resources, 407–408 resources versus settings, 30 result data types, arithmetic operators, 100–101 return types, method declaration, 128–129 reusing code, 251–252 Reverse method, 778 RoutedEventArgs object, 218–219 routines, 121 RSACryptoServiceProvider class, 614–619 RSADecrypt method, 616–617 RSAEncrypt method, 616 RSS (Really Simple Syndication), 551, 857 S sbyte data type, 55 scope (variables), 54 block scope, 79–80 class, 81 method scope, 80 restricting, 81 script running, reflection and, 593–596 sealed keyword, 126–127, 262–263, 272–273 seed values, integers, 604 select clause, 175–177 sensitive data, 515 SequenceEqual function, 767 serialization, 534, 563 binary, 576–577, 755, 863 deserialization, 534 Deserialize method, 564 JSON, 573–574, 754, 861 controlling, 575–576, 862 performing, 574–575, 861–862 namespaces, 564 parameterless constructor, 564 Serialize method, 564 908 www.it-ebooks.info Serialize method – statements XML, 859–860 attributes, 755–756 controlling, 569–573, 860–861 performing, 565–569 Serialize method, 564 set method, 85–88 set methods, LINQ, 182 SetRegistryValue method, 400 settings versus resources, 30 ShapesPaginator class, 382–385 Shell32.Shell interface, 440–443 SHEmptyRecycleBin API function, 439–440 shift operators, arithmetic operators, 101 short data type, 55, 70 short-circuit evaluation, 104 ShowDialog method, 361, 382 side effects, 105 signing, 603 Single method, 183, 766 SingleOrDefault method, 183, 766 Skip method, 183, 766 SkipWhile method, 183, 766 SMS (Short Message Service), 460 SMTP (Simple Mail Transfer Protocol), 460 SOAP (Simple Object Access Protocol), 551, 857 SolidBrush class, 791 Solution Explorer, 17 App.config, 30 AssemblyInfo.cs, 28 bin, 30 Class View, 31 files, hidden, 28–31 Form1, 30 Form1.cs, 30 Form1.Designer.cs, 30 obj, 30 Program.cs, 30 references, 30 Resources.Designer.cs, 28 Settings.Designer.cs, 28 Team Explorer, 31 solutions (Visual Studio), 11 projects, 12, 27 console, 12 Windows Forms, 12 Windows Store, 12 WPF, 12 sln file extensions, 12 suo file extensions, 12 solutions to exercises, 625–731 Sort method, 778 SortedDictionary class, 336 SortedList class, 329–331, 336, 779 specialized collections ArrayList class, 779 Hashtable class, 779 HybridDictionary class, 779 ListDictionary class, 779 NameValueCollection class, 779 Queue class, 779 SortedList class, 779 Stack class, 779 StringCollection class, 779 StringDictionary class, 779 SQL (Structured Query Language), 522 stack, 277 Stack class, 336, 779 Pop method, 332 Push method, 332 stacks, 331–333 popping items off, 331–333 pushing items on, 331–333 statements break, 165 case, 154 continue, 165–166 decision (conditional), 151 event, 290–291 namespace, 39–42 new, 66–67 operator, 112–113 partial class, 42 statement lambdas, 137 throw, 762 909 www.it-ebooks.info static – strings static, variable declaration and, 61 static events, 296 static keyword, 64–65, 125 static method, 14 operator overloading, 748 static methods, 298–300 static variables, 64–65 static volatile keyword, 64–65 Step Into command (Debug menu), 232 Step Out command (Debug menu), 233 Step Over command (Debug menu), 232 Stop Debugging command (Debug menu), 232 Stream class, 412 methods, 413, 815 BeginRead, 815 BeginWrite, 815 Close, 815 EndRead, 815 EndWrite, 815 Flush, 815 Read, 815 ReadByte, 815 Seek, 815 SetLength, 815 Write, 815 WriteByte, 815 properties, 412–413 CanRead, 814 CanSeek, 814 CanTimeout, 814 CanWrite, 814 Length, 814 Position, 814 ReadTimeout, 814 WriteTimeout, 814 StreamReader class, 412, 421–422, 818 streams, 411 AppendText method, 422 BinaryReader class, 412, 416–418, 815–817 BinaryWriter class, 412, 416–418, 815–817 classes, 412, 813–814 BinaryReader, 813 BinaryWriter, 813 BufferedStreams, 813 CryptoStream, 813 custom, 423 FileStream, 813 MemoryStream, 813 NetworkStream, 813 Stream, 813 StreamReader, 814, 818 StreamWriter, 814, 818 StringReader, 814, 818 StringWriter, 814, 818 TextReader, 814 TextWriter, 814 CreateText method, 422 downloading, 451 Exists method, 422 FileStream class, 412, 414–415 MemoryStream class, 412, 415–416 OpenText method, 422 Stream class, 814–815 StreamReader class, 412, 421–422 StreamWriter class, 412, 421–422 StringReader class, 412, 419–420 StringWriter class, 412, 419–420 text file stream methods, 818–819 TextReader class, 418–419, 817–818 TextWriter class, 418–419, 817–818 StreamWriter class, 412, 421–422, 818 string, intern pool, 58 string data type, 55 StringBuilder class, 110 StringCollection class, 324–325, 779 StringDictionary class, 329, 779 StringFormat object, 793 StringReader class, 412, 419–420, 818 strings downloading, 449–450 immutable, 58 structure, 58 910 www.it-ebooks.info StringWriter class – Task class StringWriter class, 412, 419–420, 818 strong typing, 330 generics and, 344 struct data type, 56 structures accessibility clause, 275 arrays, 276 attributes, 275 versus classes, 275 classes, choosing between, 282 container types, 269 declaring, 774 instantiation, 285 memory, requirements, 276 object assignment, 277 parameter passing, 277–281 partial keyword, 275 value types, 275 variable initialization, 66–67 subclassing, 250 subprocedures, 121 subroutines, 121 Switch method, 353 switch statement, 153–156, 757–758 enumerated values, 156–157 symmetric key encryption, simple encryption/ decryption, 608–611 syntax enumerated type, 89 variable declaration accessibility, 61, 63–64 attributes, 61, 62–63 const, 61 conventions, 59 name, 61, 62 readonly, 61 static, 61 type, 61 volatile, 61 System namespace, 305 System.Collections namespace, ArrayList class, 321–324 System.Collections.Generic namespace, 335–337 System.Collections.Specialized namespace dictionary classes, 327 StringCollection class, 324–325 System.CollectionsSpecialized namespace, NameValueCollection class, 325–326 System.Environment class methods, 396–397 properties, 395–396 System.Xml.Serialization namespace, 63 T T type, 345 default values and, 352 generic constructor, 346 Take method, 183, 766 TakeWhile method, 183, 766 Task class methods, 496–497 ConfigureWait, 846 ContiueWith, 846 Delay, 846 Run, 846 RunSynchronously, 846 Start, 846 Wait, 846 WaitAll, 846 WaitAny, 846 WhenAll, 846 WhenAny, 846 properties, 496 Exception, 845 Factory, 845 IsCanceled, 845 IsCompleted, 846 IsFaulted, 846 Status, 846 911 www.it-ebooks.info task coordination – try catch blocks task coordination deadlocks, 501–503 race conditions, 499–501 tasks, 845–847 Team Explorer, 31 templates, using directive, 307–310 ternary operators, 106–107 testing regular expressions, 478 for undesirable conditions, 213–215 text, drawing, 371–372 text file stream methods, 818 AppendText, 819 CreateText, 819 Exists, 819 OpenText, 819 text messages, 460–461 TextReader class, 418–419 methods Close, 817 Peek, 817 Read, 817 ReadBlock, 817 ReadLine, 817 ReadToEnd, 817 TextureBrush class, 791 TextWriter class, 418–419 methods Close, 818 Flush, 818 Write, 818 WriteLine, 818 this keyword, 284 Thread class, 498–499 methods Abort, 848 Join, 848 ResetAbort, 848 Sleep, 848 Start, 848 Yield, 848 properties IsAlive, 847 IsBackground, 847 Priority, 847 ThreadState, 847 thread of execution, 138 ThreadException event, 216–217 threads, 847–848 thread-safe objects, 503–504 throw keyword, 226–227 throw statement, 762 TimeSpan class, 111–112 TimeSpan data type, 747 ToArray method, 184, 767 ToBoolean function, 734 ToByte function, 734 ToChar function, 734 ToDateTime function, 734 ToDecimal function, 734 ToDictionary method, 184, 768 ToDouble function, 734 Toggle Breakpoint command (Debug menu), 233 ToInt16 function, 734 ToInt32 function, 734 ToList method, 184, 768 ToLookup method, 184, 768 toolbox, Visual Studio, 17 ToolboxBitmap attribute, 124 ToString method, 79, 264–265 TPL (Task Parallel Library), 492, 844 Parallel.For method, 492–494, 845 Parallel.ForEach method, 494–495, 845 Parallel.Invoke method, 495, 845 Trace class, 208 trace listeners, 238–240 TRACE preprocessor symbol, 211 try catch blocks, 214, 217, 220–221 catch statements, 221–222 exception objects, 223–224 exception throwing, 224–227 exceptions, custom, 227–228 912 www.it-ebooks.info TryEnter method – Type class TryEnter method, 501–502 InvokeMember method, 869 TryParse method, 78 IsAbstract property, 865 type, variable declaration and, 61 IsArray property, 865 Type class IsAssignableFrom method, 869 Assembly property, 865 IsAutoLayout property, 865 Attributes property, 865 IsByRef property, 866 BaseType property, 865 IsClass property, 866 ContainsGenericParameters property, 865 IsCOMObject property, 866 CustomAttributes property, 865 IsEnum property, 866 FindInterfaces method, 867 IsEnumDefined method, 869 FindMembers method, 867 IsExplicitLayout property, 866 FullName property, 865 IsGenericType property, 866 GetArrayRank method, 867 IsImport property, 866 GetConstructor method, 867 IsInstanceOfType method, 869 GetConstructors method, 867 IsInterface property, 866 GetCustomAttributes method, 867 IsLayoutSequential property, 866 GetDefaultMembers method, 867 IsMarshalByRef property, 866 GetElementType method, 867 IsNested property, 866 GetEnumName method, 867 IsNestedAssembly property, 866 GetEnumNames method, 868 IsNestedFamANDAssem property, 866 GetEnumUnderlyingType method, 868 IsNestedFamily property, 866 GetEnumValues method, 868 IsNestedFamORAssem property, 866 GetEvent method, 868 IsNestedPrivate property, 866 GetEvents method, 868 IsNestedPublic property, 866 GetField method, 868 IsNotPublic property, 866 GetFields method, 868 IsPointer property, 866 GetGenericArguments method, 868 IsPrimitive property, 866 GetGenericParameterConstraints IsPublic property, 866 method, 868 IsSealed property, 866 GetInterface method, 868 IsSerializable property, 866 GetInterfaces method, 868 IsSubclassOf method, 869 GetMember method, 868 IsValueType property, 867 GetMembers method, 868 IsVisible property, 867 GetMethod method, 868 MakeArrayType method, 869 GetMethods method, 868 MakeByRefType method, 869 GetNestedType method, 869 MakeGenericType method, 869 GetNestedTypes method, 869 MakePointerType method, 869 GetProperties method, 869 Module property, 867 GetProperty method, 869 Name property, 867 GetType method, 869 Namespace property, 867 GetTypeArray method, 869 StructLayoutAttribute property, 867 GetTypeFromCLSID method, 869 TypeInitializer property, 867 913 www.it-ebooks.info types – variance types constrained, 348–351 multiple, 346–348 T, 345 where clause, 351 type-safe function pointers, 93 U UI (user interface), 488–491, 843 Invoke method, 488 InvokeRequired property, 488–491 uint data type, 55, 70 ulong data type, 55, 70 unboxing, objects, 281–282 #undef directive, 31–33 undesirable conditions, 206 testing for, 213–215 UnhandledException event, 219–220 UnicodeEncoding class, 606 Union function, 767 Union set method (LINQ), 182 uploading data WebClient class, 455 WebRequest class, 455–456 URI (Uniform Resource Identifier), 447 URL (Uniform Resource Location), 447 URN (Uniform Resource Name), 447 UseResources class, 289 ushort data type, 55 using directive, 38, 41, 304–307 item templates, 309–310 project templates, 307–309 using statement, variable declaration, 739 V value equality, 113–115 value types, 275 variables, 57 values data types casting, 734–736 parsing, 736 enumerated, 156–157 generics, default, 352 parameter declaration, 82–83 parsing, 736 var keyword, 58–59 variables, 53 accessibility, 54 arrays, of arrays, 60 constant, 64–65 data types, 53, 54–57 declarations, 737–738 enumerated types, 739–740 initialization expressions, 738–739 using statement, 739 declaring multiple, 61 syntax, 59–65 environment variables, 394 setting, 394–395 initialization, 65–66 arrays, 67–68 classes, 66–67 collections, 68–69 structures, 66–67 lifetime, 54 reference types, 57 scope, 54 block scope, 79–80 class, 81 method scope, 80 restricting, 81 static, 64–65 value types, 57 var keyword, 58–59 visibility, 54 volatile, 64–65 variance, methods, 139 914 www.it-ebooks.info virtual keyword – XDocument class virtual keyword, 125–126, 259 visibility, variables, 54 Visual Studio, 3–4 code editors, Designer area, 17 downloading, editions, properties area, 17 Solution Explorer, 17 solutions, 11 projects, 12 sln files, 12 suo files, 12 Toolbox, 17 windows, 17 VisualBasic namespace, 304 volatile, variable declaration and, 61 volatile keyword, 64–65 volatile variables, 64–65 PrintDocument class, 360 BeginPrint event, 360 EndPrint event, 361 events, 360 PrintPage event, 360 QueryPageSettings event, 360 regeneration, 362 SmoothingMode, 364 W #warning directive, 34 Watch submenu (Windows submenu, Debug menu), 234 WebClient class, 447, 448–451 uploading data, 455 versus WebRequest class, 454 WebRequest class, 447 data downloads, 451–454 uploading data, 455–456 versus WebClient class, 454 WebResponse class, 447 where clause, 351 LINQ, 174–175 while loops, 160 widening conversions, 73–74, 735 Win32 namespace, 304 Windows Forms applications, 12, 16–19 exception handling, 216–217 printing, 359–360, 785–786 drawing graphics, 787–795 graphics namespaces, 786–787 Windows Store, applications, 12, 21–23 Windows submenu (Debug menu), 232, 234–235 Autos window, 234 Breakpoints window, 234, 235–237 Call Stack window, 234 Immediate window, 234, 237–238 Locals window, 234 Modules window, 235 Output window, 234 Watch submenu, 234 Winsock, 446 words (bytes), 54 WPF (Windows Presentation Foundation) applications, 12, 19–21 exception handling, 217–218 printing, 380 document creation, 795–796 FixedDocument object, 797 FlowDocument object, 796–797 paginator, 381–385, 795–796 WSDL (Web Services Definition Language), 551, 857 XYZ XAML (extensible markup language), 13 Window Designer, 19 XDocument class, 543–546 Add method, 851 DescendantNodes method, 851 915 www.it-ebooks.info XDocument class (continued) – XML XDocument class (continued) Descendants method, 851 Load method, 851 Parse method, 851 Save method, 851 ToString method, 851 WriteTo method, 851 XElement class, 543–546 Add method, 852 AddAfterSelf method, 852 AddBeforeSelf method, 852 AddFirst method, 852 Ancestors method, 852 Attribute method, 852 Attributes method, 852 DescendantAndSelf method, 853 DescendantNodes method, 852 Descendants method, 852 Document property, 851 Element method, 853 Elements method, 853 ElementsAfterSelf method, 853 ElementsBeforeSelf method, 853 FirstAttribute property, 851 FirstNode property, 851 HasAttributes property, 851 HasElements property, 851 IsAfter method, 853 IsBefore method, 853 IsEmpty property, 852 LastAttribute property, 852 LastNode property, 852 Load method, 853 Name property, 852 NextNode property, 852 Nodes method, 853 NodesAfterSelf method, 853 NodesBeforeSelf method, 853 NodeType property, 852 Parent property, 852 Parse method, 853 PreviousNode property, 852 Remove method, 853 RemoveAll method, 853 RemoveAttributes method, 853 RemoveNodes method, 853 ReplaceAll method, 853 ReplaceAttributes method, 853 ReplaceNodes method, 853 ReplaceWith method, 853 Save method, 854 SetAttributeValue method, 854 SetElementValue method, 854 SetValue method, 854 ToString method, 854 Value property, 852 WriteTo method, 854 XLink, 551, 856 XML (eXtensible Markup Language), 533 attributes, 535, 570 serialization, 755–756 CDATA section, 537–538 characters, 536–537 comments, 45–48 concise, 536 DOM (document object model), 541–546, 851–854 DTD (Document Type Definition), 551, 856 elements, nested, 535 literals, 190, 457, 854 namespaces, 538 nodes, 534 root node, 536 reading data, 854–856 XmlWriter class, 457–550 RSS (Really Simple Syndication), 551, 857 serialization, 859–861 attributes, 572–573 controlling, 569–573 parameterless constructor, 564 performing, 565–569 916 www.it-ebooks.info XmlArray attribute – XSLT SOAP (Simple Object Access Protocol), 551, 857 special characters, 849 syntax, 534–537 writing data, 849–854 WSDL (Web Services Definition Language), 551, 857 XLink, 551, 856 XmlTextReader class, 854–856 XmlWriter class, 850 XQuery, 551, 856 XSD (XML Schema Definition), 551, 856 XSL (Extensible Style Sheet Language), 551 XPath, 551, 552–555, 856 XSL FO, 551, 856 XSLT, 551, 555–559, 856 XmlArray attribute, 753 XmlArrayItem attribute, 753 XmlAttribute attribute, 753 XmlElement attribute, 753 XmlEnum attribute, 754 XmlIgnore attribute, 754 XmlReader class, 550 AttributeCount property, 855 Create method, 855 Depth property, 855 Dispose method, 855 EOF property, 855 GetAttribute method, 855 HasAttributes property, 855 HasValue property, 855 IsEmptyElement property, 855 IsName method, 855 Item property, 855 MoveToAttribute method, 855 MoveToContent method, 855 MoveToElement method, 855 MoveToFirstAttribute method, 855 MoveToNextAttribute method, 855 Name property, 855 Read method, 855 ReadInnerXml method, 856 ReadOuterXml method, 856 ReadToDescendant method, 856 ReadToNextSibling method, 856 Skip method, 856 Value property, 855 XmlRoot attribute, 754 XmlText attribute, 754 XmlTextReader class, 550, 854–856 XmlTextWriter class, 540–541 XmlType attribute, 754 XmlWriter class, 457–550 Close method, 850 Create method, 850 Dispose method, 850 Flush method, 850 WriteAttributeString method, 850 WriteCData method, 850 WriteComment method, 850 WriteElementString method, 850 WriteEndAttribute method, 850 WriteEndDocument method, 850 WriteName method, 850 WriteStartAttribute method, 850 WriteStartDocument method, 850 WriteStartElement method, 850 WriteString method, 850 WriteValue method, 850 XPath, 551, 552–555, 857–858 XQuery, 551, 856 XSD (XML Schema Definition), 551, 856 XSL (Extensible Style Sheet Language) XPath, 551, 552–555, 856, 857–858 XSL FO, 551, 856 XSLT, 551, 555–559, 856, 858 XSL FO (XSL Formatting Objects), 551 XSLT (XSL Transform), 551, 555–559, 858 917 www.it-ebooks.info Try Safari Books Online FREE for 15 days and take 15% off for up to Months* Gain unlimited subscription access to thousands of books and videos With Safari Books Online, learn without limits from thousands of technology, digital media and professional development books and videos from hundreds of leading publishers With a monthly or annual unlimited access subscription, you get: • Anytime, anywhere mobile access with Safari To Go apps for iPad, iPhone and Android • Hundreds of expert-led instructional videos on today’s hottest topics • Sample code to help accelerate a wide variety of software projects • Robust organizing features including favorites, highlights, tags, notes, mash-ups and more • Rough Cuts pre-published manuscripts START YOUR FREE TRIAL TODAY! Visit: www.safaribooksonline.com/wrox *Discount applies to new Safari Library subscribers only and is valid for the first consecutive monthly billing cycles Safari Library is not available in all countries www.it-ebooks.info ... www.it-ebooks.info www.it-ebooks.info C# 5.0 Programmer’s Reference www.it-ebooks.info www.it-ebooks.info C# 5.0 Programmer’s Reference Rod Stephens www.it-ebooks.info C# 5.0 Programmer’s Reference Published by...www.it-ebooks.info C# 5.0 PROGRAMMER’S REFERENCE Introduction xxxiii ▶▶ Part I The C# Ecosystem Chapter The C# Environment ... xxxvii xxxviii xxxviii xxxix xl xl xli Introduction xxxiii Part I: The C# Ecosystem Chapter 1: The C# Environment Visual Studio The C# Compiler The CLR The NET Framework Summary 9 Exercises 10 Chapter