<<

INDEX

■Special Characters string , 122 #define, 275 subtraction, 122 #elif, 275 unary minus, 121 #else, 275 unary plus, 121 #endif, 275 array parameters, overloading, 58–60 #if, 275 Array class, 293–294 #line, 277 arrays, 42–45 #pragma warning disable, restore, 278 conversions, 143–144 #region, 277 default size, 139 #undef, 275 description, 139 #warning, 277 initialization, 139 += operator, 221 jagged, 139, 141–142 = operator, 221 jagged or multidimensional, 15 multidimensional, 139–140 ■A rectangular, 140 Abort( ) method, multithreading, 419 reference types, 142–143 abstract members, compared with Reverse function, 144 interfaces, 85–86 sorting and searching functions, 144 abstract classes, 2, 48–51 System.Array type, 144 access reordering, 324, 326 as operator and interfaces, 88–89 accessibility of properties, 175–176 assemblies, listing types in, 465–466 accessors, 169–170 Assembly.LoadFrom( ) deployment, 444 add and remove functions, 230–238 asserts, 474–475 AddClick( ) method, 230–233 assignment operators, 125 AddEntry( ) function, updating Registry, 438 compound assignment, 125 advanced hashes, 306, 308–309 simple assignment, 125 aggregation (containment), in asynchronous operations and threading, object-oriented systems, 2 315–340 aliases, assembly name qualifier, 101–104 asynchronous calls, 331 anonymous methods, 225–227 example showing BeginInvoke( ), Append( ) method, StringBuilder class, 160 EndInvoke( ) functions, 333–334 AppendFormat( ) method, StringBuilder using delegates for multithreading, 332 class, 160 attributes, 17, 202 arithmetic operators, 121 designing, 211–212 addition, 121 finding values of, 213–215 bitwise complement, 121 multiuse, 210 division, 122 overview, 207 increment and decrement, 123 parameters of, 212–213 multiplication, 122 and relection, 213–215 remainder, 122 using, 208–211 shift, 123 AttributeTargets enumerator, 211 517 518 ■INDEX

AttributeUsage attribute, 209, 211 catch block processing, in exception autocompletion, Visual Studio .NET, 17 handling, 22–24 AutoResetEvent, ManualResetEvent class, , 281 threads, 330–331 checked and unchecked expressions, 128 checked context, 132–133 ■B checked/unchecked statement, 112 base classes class keyword, 15 and arrays, 42–45 class view, Visual Studio .NET, 18 Engineer class example, 39–40 classes in inheritance, 1 abstract, 48–51 and simple inheritance, 40–41 accessibility and virtual functions, 46–48 hierarchical resolution of method data types, 13 overloading, 56–57 BeginGetResponse( ) function, interaction with member accesibility, 55 asynchronous processing, 340 internal modifier, 53 BeginInvoke( ), EndInvoke( ) functions, internal modifier with class members, asynchronous calls, 332 53–54 BidirectionalSubrange method, 194 internal protected modifier with class BinaryReader and BinaryWriter classes, 389 members, 55 bit flag enumerations, 202 nested classes, 61–62 boolean literal, 280 public modifier, 53 boxing and unboxing, 14–15, 80–81, 146–147, base classes 501–502 and arrays, 42–45 built-in data types, 14 Engineer class example, 39–40 built-in operators, 120 and simple inheritance, 40–41 Button class, 230, 235 and virtual functions, 46–48 ButtonHandler, 230 breaking into parts (partial classes), 76–77 Button.TearDown( ), 238 creation, initialization, destruction, 62–66 ■ destruction, 65 finalizers and non-memory resources, 66 C#. See also programming languages, initialization, 65 comparison to C/C# and member functions, 33 development options for, 17 nesting, 61–62 resources and out parameters, 36 Code Project, 516 and pre- and post-conversions, 247–253 CodeGuru, 515 and reference (ref) parameters, 33–35 C-Sharp Corner, 515 sealed, 51 DotNet Books, 516 simple class example, 31–33 GotDotNet, 515 singleton, 64–65 MSDN, 515 ClearDelegates( ) method, 238 Pinvoke.NET, 516 ClickHandler delegate, 230 C# command line (csc), 17 ClickOnce, 443–445, 447 calling program exception handling, 25–27 CLS compliance, 450–451 camelCasing, 449 ClsCompliant assembly attribute, 450–451 Capacity property, StringBuilder class, 160 ClsCompliant attribute, 211 case-insensitive collections, cluster size calculations on disk, 441–443 CreateCaseInsensitiveSortedList( ), CodeDOM, execution time code generation, CreateCaseInsensitiveHashtable( ) 354–355, 357 functions, 309 CodeReviewAttribute, 209, 214 ■INDEX 519 collections, in .NET Framework, 311–312 ■D command-line compiler DataRow class, 181 default response file, csc.rsp, 493 DateTime class formats, 385 option parameters, 494–496 Debug and Trace classes, 474–475, 477 overview, 493 BooleanSwitch, 477–478 response files, 493 custom switches, 480–481, 483 simple usage, 493 TraceInformation( ), capturing process command-line compiler for C#, 17 metadata, 483–484 comments, 282 TraceSwitch, 478, 480 Common Language Runtime (CLR), 5 debugger, Visual Studio .NET, 18 Compare( ), string operation, 157 decimal point format string character, 381 CompareOrdinal( ), string operation, 157 defensive programming, overview, 473 CompareTo( ), string operation, 157 definite assignment, 114–117 comparison to C/C#, 64 with arrays, 116 Compiled option, RegEx class, 162 with structs, 115–117 compile-time compared with execution Delegate class, 220 time, 341 DelegateCache class, 238 Complex class, 209 Delegate.Combine( ), 220–221 complex parsing, example program, Delegate.Remove( ), 221 163–164, 166 delegates, 16 Component Object Model (COM), and multicasting, 220–221 disadvantages, 5 as static members, 222–223 Concat( ), string modification method, 158 as static properties, 223–225 conditional methods, Conditional attribute, using, 217–219 473–474 delegateStore , 235 Configuration file, updating, 439, 441 derived class, in inheritance, 1 const, knowable at compile time, 72–73 design guidelines constants, 72 for exception handling, 30 constraints, 148–149 for virtual functions and interfaces, 312 constructed type, in generics, 147–148 destruction, 65 constructor, private, 64–65 development options for C#, 17 constructors digit or space placeholder format string creation and initialization, 62–64 character, 380 in exception handling, 28 digit or zero placeholder format string containment (aggregation), role in character, 380 object-oriented systems, 2 disposable enumerators, 191–192 conversions, 129–138. See also user-defined Dispose( ), 66–69, 191 conversions DotGNU, 443 checked, 132–133 and overloaded functions, 130–132 ■E conversions, class, 133 encapsulation, 4 base class, 134–135 EndGetResponse( ) function, asynchronous interface, 135 processing, 340 interface implementations, 135, 137 EndInvoke( ) potential problem, 337, 339 between interface types, 137 EndsWith( ), string operation, 157 conversions, structs, 137 Engineer class example, 39, 40 CopyTo( ), string modification method, 158 EnsureCapacity( ) method, StringBuilder currency standard format string, 376 class, 160 custom format strings, 379–385 520 ■INDEX

enumerations, 15 overview, 341 base types, 200 Reflection.Emit class creation in memory, bit flag enums, 202 357, 359–361 conversions, 202–203 secondary application domain, 361, 363 initialization, 201 using CodeDOM to create code, line-style, 199–200 354–355, 357 overview, 199 explicit conversions, 129, 132 System.Enum type, 203–205 explicit interface implementation, 92–94 enumerators. See also indexing ExplicitCapture option, RegEx class, 162 disposable, 191–192 exponential notation format string, 383 and foreach statement, 185–189 extern alias improving, 189–191 external assembly alias, 102–104 and iterators, 192–198 reference switch, 103–104 environment settings, System.Environment external assembly alias, 101–104 class, 400–402 Equals( ) function, 285–286, 289–292 ■F escapes and literals, in format strings, 384 finalizers and non-memory resources, 66 EventArgs, 233 finally exception block, in exception events, 16 handling, 28, 30 add and remove functions, 230–238 FindColumn( ), 183 overview, 229–230 fixed-point format string, 377 exception handling ForEach method, of Array, 227 Caller Beware, 25 foreach statement, 185–189 Caller Confuse, 25–26 form designer, Visual Studio .NET, 18 Caller Inform, 25–27 Format( ), 159 calling program processing, 25–27 function pointers, 217, 219 catch block processing, 22–24 functions design guidelines, 30 and interfaces by .NET Framework finally exception block, 28, 30 class, 313 garbage collection and efficiency, 30 member functions, 33 hierarchical processing, 22–24 overloaded, 4, 36–37 overview, 22 performing same action, but with different type of exception matching, 22 parameters, 36–37 user-defined classes, 27–28 virtual, 46–48 execution time code generation future of C#, 516 self-modifying code polynomial FxCop programming tool, 491 evaluation example, 344–361 ■G casting class instance to an interface, 353 garbage collection class instantiation using .NET CLR, concurrent, 463–465 341–343 controlling, 463 custom class for self-modifying code, and efficiency in exception handling, 30 350–352 finalization (destructors), 65–66, 462, 463 dynamic execution time assembly generations, 461–462 discovery, 343–344 heap allocation, 460 eliminating compiler dependency, Mark and Compact algorithm, 460 354–355, 357 overview, 460 faster custom class for self-modifying server vs. workstation, 463–465 code, 353 general format string, 378 lightweight code generation model, 361, 363 generic collections, in .NET Framework, 312 ■INDEX 521 generic comparison interfaces, IComparable interface, 294–295 IComparable, IComparer, 302, IComparer interface, 295–298, 300 304–306 IDisposable interface, 66–69, 191 generic methods, 150 IEnumerable.GetEnumerator( ), 190 constraints, 150 IEnumerator interface, 186, 188–190 specifying the type argument, 150 Iformattable interface, custom object generics formatting, 386–387 allowed and prohibited examples, 151–152 IgnoreCase option, RegEx class, 162 boxing, elimination, 155 IgnorePatternWhitespace option, RegEx compared with C++ templates, 145, 149 class, 162 compile-time type-safety, 155 IhashCodeProvider interface, 306, 308–309 constraints, 148–149 IL Disassembler (ILDASM), Visual Studio constructed type, 147–148 .NET, 18–19 default keyword, 153 IL language coding, using Reflection.Emit, design guidance, 155 357, 359–361 example of program using object instead immutable classes, 82–83 of generics, 146 immutable type, strings, 157 inheritance, overriding and implicit conversions, 129, 253 overloading, 151 index function, strings, 157 interfaces, delegates, and events, 152–154 indexer, 16 vs. nongeneric collections, 314 IndexerNameAttribute, 192 overview, 145 indexing. See also enumerators type parameter, 146 indexer design guidelines, 192 using reflection with, 471–472 with integer index, 179–181 GetCustomAttributes, 214–215 with multiple parameters, 183–185 GetDecoder( ), encoding methods, 158 with string index, 181–183 GetDiskFreeSpace( ) function, 441, 443 IndexOf( ), string operation, 158 GetEncoder( ), encoding methods, 158 inheritance, 40–41 GetEnumerator( ) method, 189, 190, 194 and interfaces, 89–91 GetHashCode( ) function, 286, 288 in .NET CLR (Common Language group separator format string character, 381 Runtime), 2 role in object-oriented systems, 1–2 ■H initialization, classes, 65 hashes input/output advanced, 306, 308–309 BinaryReader and BinaryWriter classes, 389 and GetHashCode( ) function, 286, 288 detecting process completion, 393 Hashtable class, 234, 286, 288 reading and writing files, 390 Hello World, initial C# program, 11 redirecting process output, 392 helper classes, 61–62 serial ports, System.IO.Ports format string, 379 namespace, 389 hierarchical processing, in exception starting processes, Process class, 392 handling, 22–24 Stream class, 388–389 hierarchical resolution of method StringReader and StringWriter classes, 389 overloading, 56–57 TextReader and TextWriter classes, 389 HttpWebRequest class, asynchronous traversing directories, 390–392 processing, 340 XmlTextReader and XmlTextWriter ■I classes, 389 Insert( ) method ICloneable interface, MemberWiseClone( ) string modification method, 158 function, 309, 311 StringBuilder class, 160 522 ■INDEX

instantiation, preventing, 64–65 invoking functions, implementing integer index, 179–181 late-bound architecture, 467–470 , 280 is-a relationship, in inheritance, 2 Intellisense, Visual Studio .NET, 17 IsGenericTypeDefinition property, 471–472 interfaces iteration statements, 107 and abstract class design guidelines, 90–91 do, 108–109 checking type support, 86–87 for, 109 combining, 95–96 foreach, 110–111 compared with abstract methods, 86 while, 107–108 description, 85 implementation ■ example, 85–86 Java, comparison to C/C# hiding, 95 attributes, 505 multiple, 91–94 base classes and constructors, 502 and inheritance, 89–90 classes, 502 as operator, 88–89 constants in classes, 502 and structs, 96–97 data types, 500–501 syntax requirements, 86 delegates and events, 505 internal class modifier, 53–55 documentation options, 507 interop enumerations, enum type, 504 being used by COM objects, 365 generics, 507 calling ReadFile( ) from kernel32.dll identifying types, 504 safe version, 368–369 import vs. using, 506 unsafe version, 366, 368 interfaces, 505 choosing C# (platform invoke) or internal accessibility of members, 504 managed C++, 374 object data types, 501–502 design guidelines, 374 operator overloading, 504 fixed statement, 368 overflows, 506 fixed-size buffers, 371–372 properties and indexers, 505 functions using Structure parameter, ref and out parameters, 504 369–370 statements, 506 intPtr, 365–366 static constructors, 503 marshalling in C#, 374 strings, 506 .NET, language independence, 10 unsafe code, 506 overview, 365 value and reference types, 502 platform invoke, 365–366 virtual functions, hiding, and versioning, pointers and declarative pinning, 366, 503–504 368–369 Join( ), string modification method, 158 preventing garbage collection, 374 jump statements, 111 proxies (wrapper) classes, 365 break, 111 StructLayout attribute, 369 continue, 112 structure layout, 369 goto, 112 tlbimp utility and COM objects, 365 return, 112 using COM objects, 365 Just-In-Time (JIT) compiler, 19 using degates for Windows callback ■L functions, 372–373 languages, programming. See programming IntList.GetEnumerator( ), 190 languages, comparison to C/C# intPtr, 365–366 LastIndexOf( ), string operation, 158 invocation list, 221 late-bound architecture, 467–470 ■INDEX 523 left-associative operator precedence, 119 Multiline option, RegEx class, 163 Length property, StringBuilder class, 160 MultilineRightToLeft option, RegEx class, 163 lexical details multiple implementations of interfaces, boolean literal, 280 91–94 character literal, 281 multiple inheritance, 2, 15 comments, 282 multiple sort orders, 295–298, 300 identifiers, 279 multithreading, 417–418, 419 integer literal, 280 multiuse attributes, 210 keywords, 279–280 Mutex, use in multithreading, 323 literals, 280 mutexes and semiphores, exclusion overview, 279 primitives in multithreading, real literal, 280 322–323 , 281 verbatim string literal, 282 ■N Liberty, Jesse, 497 namespaces, 12, 13 libraries, disadvantages, 5 naming conflict, 113–114 line-style enumerations, 199–200 Native Code Generator (NGEN), Visual literals, 280 Studio .NET, 19 Load( ), 181 nested classes, 61–62 lock method, singleton instantiation, 64–65 .NET Framework lock statement, 112, 321 assemblies, 9 Longer-Lived Objects and IDisposable, 69 attributes, 10 looping statements, 107 ClickOnce deployment, 8 do, 108–109 COM and library interface, 5 for, 109 custom object formatting, 386–387 foreach, 110–111 error handling, 6 while, 107–108 execution environment, 6 format strings ■M currency standard, 376 Main( ) function, 12 custom date and time, 386 managed environment, 8 custom strings, 379–385 matching exception types, in exception date and time, 385 handling, 22 decimal point character, 381 MaxCapacity property, StringBuilder class, 160 decimal standard, 376 member functions, 33 digit or space placeholder character, 380 MethodImpl attribute, 235 digit or zero placeholder character, 380 methods escapes and literals, 384 abstract, compared with interfaces, 86 exponential notation, 383 anonymous, 225–227 fixed-point standard, 377 sealed, 51 formatting, overview, 375 Microsoft IL Code (MSIL), 19 general standard format string, 378 modifier interaction of classes and class group separator character, 381 members, 55 hexadecimal standard format string, 379 Monitor class, using for multithreading, number prescaler character, 382 320–321 number standard, 378–379 Mono, 443 numeric formatting built-in types, 375 Most Recently Used list (MRU), 437–438, 441 percent format string character, 382 MoveNext method, 192 scientific (exponential) standard format mscorlib, 13 string, 377 multicasting, 220, 221 section separator character, 383 524 ■INDEX

language independence, 6, 10 ■O language interop, 10 object, definition, 1 deployment and packaging, 8 Object Browser, Visual Studio .NET, 18 design guidelines, 289–292 OnClick( ) method, 230 Equals( ) function, 285–292 OnKey method, 233 garbage collection, 8 operator overloading Hashes and GetHashCode( ) function, bad example, 261 286, 288 binary operators, 259 input/output, 388 complex number example, 262, 264–265 language expressibility, 10 example, 261 metadata, 9 good example, 261 namespaces, 6, 8 guidelines, 261 NumberFormatInfo class, 379 overview, 259 numeric parsing of strings, TryParse, 385 restrictions on, 261 numeric parsing, Parse( ) method, 387 simple example, 260 operator==( ), operator!=( ) functions, 286 unary operators, 259 round-trip format, 379 operator precedence, 119–120 safety and security, 8 operator!=( ) function, 286 System namespace, 6 operator==( ) function, 286 System.Data namespace, 7 optimize+ compiler flag, 472 System.XML namespace, 7 option, 274–275 ToString( ) function, 283–285 accessing command-line parameters with universal .NET Framework functions, 283 string array, 274 verified environment, 8 multiple Main( ) functions, 274–275 XML, System.Data.XML and System.XML programming overview, 273 namespaces, 388 returning status with int variable, 273 .NET SDK (Software Developer Kit), 17 ordering instances, 294–295 /nowarn switch in preprocessing, 278 out parameters, 227, 36, 56 coalescing operator (??), 270–271 outer variables, 227 Nullable , 268 overloading nullable types conversion rules for resolution, 57–58 C# language types, 268 example, 55–56 compared with SQL nulls, 269–270 functions, 36–37 design guidelines, 270 method hiding, 56–57 earlier nullability programming method rules, 55–56 options, 267 relational operators, 301–302 null coalescing operator (??), 270–271 variable-length parameter lists, 58, 60 Nullable methods available, 268 override keyword, 47 Nullable value type, 268 overview, 267 ■P statement examples, 269 PadLeft( ) string modification method, 158 number format string, 378–379 PadRight( ) string modification method, 158 number prescaler format string character, 382 parameterized types, 145 number standard, adding multithreading, parameters, functions with different (yet 417–418 performing same action), 36–37 NumberFormatInfo class, 379 params keyword, 58, 60 numeric conversions, 129 Parse( ) method, numeric parsing, 387 numeric parsing of strings, TryParse, 385 partial classes, 76–77 NUnit programming tool, 490 PascalCasing, 449 ■INDEX 525 percent format string character, 382 static constructors, 503 polymorphism, role in object-oriented strings, 506 systems, 2 unsafe code, 506 PreJIT (Pre Just-In-Time) compiler, 19 value and reference types, 502 preprocessing virtual functions, hiding, and #define, 275 versioning, 503–504 #elif, 275 lack of pointers, 497 #else, 275 lack of runtime libraries, 498 #endif, 275 managed environment, 497 #if, 275 .NET objects, 498 #line, 277 overview, 497 #pragma warning directive, 278 statements, 498 #region, 277 use of exception handling, 498 #undef, 275 use of iterators, 499 #warning, 277 use of nullable types, 499 example, 276 versioning, 499 expressions, 276–277 Visual Basic, 6, 507 inline warning control, 278 arrays, 509 overview, 275 classes, types, functions and /warnaserror switch, 278 interfaces, 510 programming languages, comparison to code appearance, 507–508 C/C# control and program flow, 510 anonymous methods, 498 conversions, 508–509 assembly output of compiler, 497 data types and variables, 508 attributes, 499 Do loops, 511 code organization, header files and For Each loops, 511 macros, 499 On Error, 512 features missing in C#, 499 functions, 510 garbage collection not predictable, 497 If Then, 510 Java, 500 For loops, 511 attributes, 505 missing statements, 512 base classes and constructors, 502 operators and expressions, 509 classes, 502 Select Case, 512 constants in classes, 502 strings, 509 data types, 500–501 Visual C++ and Visual Basic, 513 delegates and events, 505 projects, Visual Studio .NET, 18 documentation options, 507 properties enumerations, enum type, 504 accessibility of, 175–176 generics, 507 accessors, 169, 169–170 identifying types, 504 and inheritance, modifiers, 170 import vs. using, 506 overview, 16, 169 interfaces, 505 property, StringBuilder class, 160 internal accessibility of members, 504 property use object data types, 501–502 example program, 170–171 operator overloading, 504 example program demonstrating overflows, 506 accessibility, 175–176 properties and indexers, 505 example program demonstrating ref and out parameters, 504 efficiency, 175 statements, 506 526 ■INDEX

example program triggering new actions, secondary application domain, 361, 363 172–173 section separator string character, 383 example program with static properties, SecureString type, 166–168 173–174 SecureString type, example program, 166–167 example program with virtual selection statements, 105 properties, 177 if, 105 protected access, 40 switch, 105, 107 proxies (wrapper) classes, 365 Semiphore, use in multithreading, 323 public class modifier, 53 serial ports, System.IO.Ports namespace, 389 serialization ■R binary format, 393, 395–396 read only fields, 72–75 BinaryFormatter and SoapFormatter, 427 reading web pages, overview, 398–399 custom, 396–398 real literal, 280 description, 393 ref (reference) parameters, 33–35, 56, 227 ISerializable interface, 427, 429 reference data types, 14 Serializable, NonSerialized attributes, 393, reflection, 17 395–396 Reflection.Emit execution time code and state, 425–427 generation, 357, 359–361 XML format, 393, 395–396 RegEx, 161, 163 Singleline option, RegEx class, 163 RegEx class, example program, 161–162 singleton class, in multithreading, 327–8 Registry, updating, 438 sorting and searching, 293–294 regular expressions, 161, 163 Split( ) relational and logical operators, 123 example program, 159–160 conditional operator, 125 string modification method, 158 logical negation, 123 SQL nulls compared to C# nulls, 269 logical operators, 124–125 standard format string, 376 question operator, 125 starting processes, Process class, 392 relational operators, 124 StartsWith( ), string operation, 158 ternary operator, 125 statements, C# similar to C++, 15 relection, and attributes, 213–215 static classes, 75–76 Remove( ) method static constructors, 71 string modification method, 158 static fields (static members of classes), StringBuilder class, 160 69–70 remove function, 230–8 static function, 12 RemoveClick( ), 232–233 static member functions, 70–71 Replace( ) method string index, 181–183 string modification method, 158 string interning, 107 StringBuilder class, 160 string literal, 281 Reset method, 193 StringBuilder class, example program, 160–161 resource management, 66, 68–69 StringReader classes, 389 return codes, errors created by, 21 strings return values, 334–335 compare and search operations right-associative operator precedence, 119 Compare( ), 157 round-trip format specifier, 379 CompareOrdinal( ), 157 ■S CompareTo( ), 157 EndsWith( ), 157 scientific (exponential) format string, 377 IndexOf( ), 158 screen scraper example, 398–399 LastIndexOf( ), 158 sealed classes and methods, 51 StartsWith( ), 158 ■INDEX 527 encoding methods MaxCapacity property, 160 GetDecoder( ), 158 Remove( ) method, 160 GetEncoder( ), 158 Replace( ) method, 160 example program ToString( ), 159 complex parsing, 163–164, 166 , 158 RegEx class, 161–162 StringWriter class, 389 SecureString type, 166–167 struct keyword, 15 SecureString type indirect StructLayout attribute conversion, 167 LayoutKind Auto, 369 Split( ), 159–160 LayoutKind Explicit, 369 StringBuilder class, 160–161 LayoutKind Sequential, 369 Format( ), 159 structs modification methods all-zeroed state, 81–82 Concat( ), 158 and constructors, 81 CopyTo( ), 158 description, 79 Insert( ), 158 example of point struct, 79–80 Join( ), 158 instantiating without new, 81–82 PadLeft( ), 158 and interfaces, 96–97 PadRight( ), 158 use only for data types, 82 Remove( ), 158 user-defined conversions between, Replace( ), 158 242–247 Split( ), 158 style in programming Substrng( ), 158 encapsulation, 450 ToLower( ), 158 FxCop programming tool, 491 ToUpper( ), 158 generic-based collections, 488 Trim( ), 158 ignoring exceptions, 488 TrimEnd( ), 158 naming, 449–450 TrimStart( ), 158 author recommendations, 485–486 operations, 157 camelCasing, 449 overview, 157 class naming, 451 RegEx class Hungarian, 450 Compiled option, 162 Microsoft naming recommendations, 485 ExplicitCapture option, 162 PascalCasing, 449 IgnoreCase option, 162 NUnit programming tool, 490 IgnorePatternWhitespace option, 162 overview, 449 Multiline option, 163 processor memory models, 489 MultilineRightToLeft option, 163 use throw instead of throw ex, 488 Singleline option, 163 using blocks, 488 regular expressions, 161, 163 using exception handling, 486–487 security using Monitor locks and the lock SecureString type, 166, 168 statement, 489 SecureString type methods, 167 using threads and thread-safety, 489 StringBuilder class using Visual Studio .NET IDE, 486 Append( ) method, 160 Substrng( ), string modification method, 158 AppendFormat( ) method, 160 synchronized collections, Synchronized( ) Capacity property, 160 method, 309 EnsureCapacity( ) method, 160 System.Attribute, 209 Insert( ) method, 160 System.Enum type, 203–205 Length property, 160 System.Environment class, 400–402 528 ■INDEX

■T user-defined classes, in exception handling, TextReader and TextWriter classes, 389 27–28 this. syntax, 32 user-defined conversions threading and asynchronous operations conversion lookup, 256–257 and access reordering, 324–328 design guidelines, 253–256 asynchronous calls, 331–340 example, 239–241 joining threads, 329–330 overview, 239 using WaitHandle, 330–331 pre- and post-conversions, 241–242, volatile, 327–328 247–253 example of multithreading bug, 315–319 between structs, 242–247 overview, 315 user-defined data types, 14 thread-safe techniques, 319–323 user-defined operators, 121 threads User.Process( ), 220 description, 328 Using and IDisposable, 68 using Join( ) method, 329–330 using keyword, 11–12, 13 using WaitHandle abstract class, 330–331 using statement, 112 thread-safe techniques, 319 avoiding data sharing, 320 ■V interlocked operations, 322 value types, 14 potential problem with EndInvoke( ), variable scoping, 113–114 337, 339 variable-length parameter lists, 58, 60 synchronized method, 322 variables, outer, 227 using exclusion primitives verbatim string literal, 282 Monitor class, 320–321 versioning, 99 mutexes and semiphores, 322–323 C# code assists, 101 using immutable objects, 320 example, 99–101 waiting for thread completion, 336–337 .NET innovations, 99–101 ToLower( ), string modification method, 158 override modifier, 100 ToolTips, 441 virtual functions, 100 ToString( ) function, 159, 203, 283–285 virtual functions, 46–48 ToUpper( ), string modification method, 158 example, 3–4 TreeView control, adding icons, 420 and interfaces, design guidelines for, 312 Trim( ) string modification method, 158 virtual properties, 177 TrimEnd( ) string modification method, 158 Visual Basic TrimStart( ) string modification method, 158 comparison to C/C#, 6, 507 try-catch-finally statement, 112 arrays, 509 type, finding the members of, 466–467 classes, types, functions and type operators, 126 interfaces, 510 as, 127 code appearance, 507–508 is, 126–127 control and program flow, 510 type of, 126 conversions, 508–509 type parameter in generics, 146 data types and variables, 508 type support checking with interfaces, 86–87 Do loops, 511 For Each loops, 511 ■U On Error, 512 Unicode, 158 functions, 510 unsafe If Then, 510 description, 451 For loops, 511 example code, 451–455 missing statements, 512 ■INDEX 529

operators and expressions, 509 output type, changing to Console Select Case, 512 Application, 410 strings, 509 populating TreeView control, 410–411 Visual C++ and Visual Basic, comparison to refactoring to correct program C/C#, 513 updating, 436 Visual Studio .NET, 17–9 saving and restoring program state, volatile keyword, 324, 326, 327–328, 419 425–426 serialization and state, 425–427 ■W sorting files, 424 WaitHandle abstract class, threads, 330–331 StatusBar control, 412, 414–415 /warnaserror switch in preprocessing, 278 System.Array class, 424 Windows Forms, DiskDiff example System.Windows.Forms.Form, 404 program, 403 tracking program progress, 412, 414–415 abstract classes for file manipulation, TreeView control, 406 433–434 updating control with Control.Invoke, 418 adding cancel button, 420 updating TreeView for delete operations, adding directory comparison, 431 435, 436 adding file manipulation, 432 using form designer, 406 adding icons to TreeView, 420 Windows Forms, overview, 403 adding keyboard accelerators, 437 WriteIf( ), WriteLine( ), debugging, 475, 477 adding Most Recently Used list (MRU), 437–438, 441 ■X adding ToolTips, 441 XCOPY deployment, 443 calculating cluster size on disk, 441, 443 XML calculating directory sizes, 408–409 documentation ClickOnce deployment, 444–445, 447 compiler-support tags, 455–456, 458, customizing TreeView icon display, 431 459–460 debugging using Console Application, 410 overview, 455 demand triggered TreeView population, supported tags, 458–459 422–423 serialization, 425–427 deploying the program, 443–445, 447 XML, System.Data.XML and System.XML DirectoryInfo class, 406, 408 namespaces, 388 file and directory operation code, 434–435 XmlTextReader and XmlTextWriter FolderBrowserDialog class, 412 classes, 389 form layout and control code, 404, 406 InitializeComponent( ) method, 406 ■Y interrupting a thread with polled internal yield return statements, 193–195 flag, 419