He regularly publishes articles on security design and is the co-author of six security books, including the award-winning Writing Secure Code, 19 Deadly Sins of Software Security, The
Trang 2C# 3.0: The Complete Reference
Trang 3About the Author
Herbert Schildt is a leading authority on C#, C++, C,
and Java His programming books have sold more than 3.5 million copies worldwide and have been translated into all major foreign languages He is the author of
numerous bestsellers, including Java: The Complete
Reference, C++: The Complete Reference, C: The Complete Reference, and C#: A Beginner’s Guide Although interested
in all facets of computing, his primary focus is computer languages, including compilers, interpreters, and robotic control languages He also has an active interest in the standardization of languages Schildt holds both graduate and undergraduate degrees from the University of Illinois
He can be reached at his consulting office at (217) 586-4683
His web site is www.HerbSchildt.com.
About the Technical Editor
Michael Howard (Austin, Texas) is a principal security
program manager on the Trustworthy Computing (TwC) Group’s Security Engineering team at Microsoft, where he is responsible for managing secure design, programming, and testing techniques across the company Howard is an architect of the Security Development Lifecycle (SDL), a process for improving the security of Microsoft’s software Howard speaks regularly on the topic of securing code for Microsoft and at conferences worldwide He regularly publishes articles on security design and is the co-author of six
security books, including the award-winning Writing Secure Code, 19 Deadly Sins of Software Security, The Security Development Lifecycle, and his most recent release, Writing Secure Code for Windows Vista.
Trang 4C# 3.0: The Complete Reference
Herbert Schildt
New York Chicago San Francisco Lisbon London Madrid Mexico City
Milan New Delhi San Juan Seoul Singapore Sydney Toronto
Trang 5Copyright © 2009 by The McGraw-Hill Companies All rights reserved Except as permitted under the United States Copyright Act of 1976,
no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher
ISBN: 978-0-07-159842-2
MHID: 0-07-159842-6
The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-158841-6, MHID: 0-07-158841-8
All trademarks are trademarks of their respective owners Rather than put a trademark symbol after every occurrence of a trademarked name,
we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark Where such designations appear in this book, they have been printed with initial caps
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs To contact a representative please visit the Contact Us page at www.mhprofessional.com
Information has been obtained by McGraw-Hill from sources believed to be reliable However, because of the possibility of human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information
TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc (“McGraw-Hill”) and its licensors reserve all rights in and to the work Use
of this work is subject to these terms Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited Your right to use the work may be terminated if you fail to comply with these terms
THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DIS-CLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom McGraw-Hill has no responsibility for the content of any information accessed through the work Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise
Trang 6I n d e x 885
constrained, 504 See also
Constraint(s)
constructed See Constructed type
data See Data types
extensibility, 213, 240
identification, runtime, 459–463
inference, 416, 526, 527, 549–550
metadata, 457
nullable, 36, 518, 519
numeric, NET structures for the
built-in, 397–397
open, 499
parameterized, 495
promotion, 59–60
reference See Reference types
safety and generics, 495, 496,
498, 499–502, 506, 526, 529,
544, 641, 660, 750, 775, 800, 801
value See Value types
Type argument(s), 498, 499
and constraints, 503–521 See also
Constraint(s)
and pointer types, 544
to call a generic method, using
explicit, 527
Type class, 462, 463, 465, 485
commonly used methods
defined by the, list of, 464
commonly used properties
defined by the, list of, 464
Type conversion, 42, 55–62
and casting, 55, 56–58, 60–62
implicit (automatic), 55–56, 59,
190–192
Type parameter(s), 497–498, 544
comparing instances of a,
534–537
and constraints, 503–504
creating a default value of a,
522–523
and generic class hierarchies,
537–540
and generic delegates, 527
and generic interfaces, 530,
533, 534
and generic methods, 524–526
overloading methods that use,
542–543
relationship between two,
establishing a, 520
TypeCode enumeration, 624, 626,
628, 632, 636, 640
typeof operator, 462–463,
467, 476
U
uint value type, 36, 37, 38, 396, 623 literal, 46
UInt16 NET structure, 396, 397, 623 UInt32 NET structure, 396, 397, 623 UInt64 NET structure, 396, 397, 623 ulong value type, 36, 37, 396, 623 literal, 46
Unboxing, 307–309 unchecked keyword, 360–362
#undef directive, 451, 455 Unicode, 41, 363, 378 UTF–16 and UTF–32, 634–635 UnicodeCategory enumeration, 636 Uniform Resource Identifier (URI), 823
constructing a, 833 dissecting a, 833–834 Uniform Resource Locator (URL), 823 UnionWith( ), 798
Unmanaged code, 9, 585, 586 UnmanagedMemoryStream class, 365 /unsafe compiler option, 586 unsafe keyword, using, 587–588 UploadData( ), 844, 845 UploadFile( ), 845 UploadString( ), 845 UploadValues( ), 845 Uri class, 823, 833–834 UriFormatException, 825, 830 UseDefaultCredentials property,
826, 843 ushort value type, 36, 37, 38, 396, 623 using directive
to bring a namespace into view,
20, 22–23, 441–443
to create an alias, 443–444 using statement, 374, 607–608
V
ValidOn property, 491 value
contextual keyword, 33 implicit parameter, 246, 248–249,
255, 425 428 Value property, 599, 755, 778, 783,
788, 791, 809, 836–837 Value type(s), 35–36, 115 boxing and unboxing, 307–309 and call-by-reference, 176–181 constraints, 504, 517, 519 default value of, 127, 522 new and, 130–131 null and, 518
passed to methods, 174–175 structures as, 311, 326, 330, 331 structures corresponding to the built-in numeric, NET, 396–397, 623–641 table of, 36 Values property, 754, 762, 765, 777,
788, 790, 793 Values<TV> property, 777 var keyword, 51, 151, 153, 547, 548,
550, 571 Variable(s) captured, 412–413, 416 const, 607
declaration, 23–24, 25, 28, 49, 53 definition of, 23
dynamic initialization of, 50–51 implicitly typed, 7, 25, 51–52,
547, 569, 571 initializing, 50–51, 53–54
instance See Instance variables
local, 49, 52–53 member, 12 names, rules governing, 33–34 outer, 412–413, 416
pointer See Pointer(s)
query, 547, 548
reference See Reference
variable(s) scope and lifetime of, 52–54 static, 205–206, 208–209 volatile, 607
Version class, 835 virtual keyword, 294, 297, 301 Virtual
methods, 291, 294–301, 305, 540–542
properties, 297 Visual C++ 2008 Express Edition, 15 Visual C# 2008 Express Edition, 14, 15 Visual Studio IDE (Integrated Development Environment), 14, 15–19, 848, 851, 852, 867 void, 21, 116
methods, returning from, 119–120 volatile modifier, 607
vsvars32.bat file, 15
W
Wait( ), 723–727, 746 WaitForExit( ), 748 WaitForPendingFinalizers( ), 658 WaitHandle class, 730, 734 WaitOne( ), 730, 733, 734, 737, 739
Trang 7886 C # 3 0 : T h e C o m p l e t e R e f e r e n c e
#warning directive, 451, 456
warning #pragma option, 457
Warth, Chris, 4
Web crawler
definition of, 839
example program, 839–842
WebClient class, 822, 823, 842–845
methods, table of synchronous,
844–845
properties, table of, 843
WebException, 830–831, 842
WebExceptionStatus
enumeration, 831
WebHeaderCollection collection, 834
WebRequest class, 823, 824–826, 829,
830, 842, 845
methods, table of synchronous
communication, 825
properties, table of, 825–826
WebResponse class, 823, 824, 826,
830, 831, 834, 842, 845
methods, table of commonly
used, 827
properties, table of, 827
where clause, 504, 505, 512, 517,
520, 521
in a LINQ query, 547–548,
551–552
Where( ) extension method, 574,
575–576
while loop, 85, 99–101, 105 Widening conversion, 55, 56 Width property, 855, 863 Window, form-based changing default size of, 863 illustration of skeletal, 850 Windows, 6, 8, 9
API, 847–848, 851 controls, 849, 852, 855
form See Form, Windows
program, compiling, 851 Windows application, form-based adding button to, 852–853 adding menu to, 859–866 compiling, 851
message handling, 853–856 skeleton, 849–851
threading model, 851 using message box in, 856–858 Windows Forms NET subsystem,
847, 848, 850 Windows programming form-based, 847–866 history of, 847–848 and message-based interaction, 849 using Visual Studio, 848 Write( ), 24–25, 364, 366, 371, 375, 384 WriteByte( ), 364, 375
WriteLine( ), 21, 24–25, 26, 41, 43, 48,
307, 351, 366, 371, 394, 395, 396, 640 formatted output version of, 43–46, 685
using a method within, 122 WriteTimeout property, 365
X
X property, 852 XML, 545, 546 XML comment, 867 tags, table of, 868 XML file containing, 869–870 XOR (exclusive OR) operator (^) bitwise, 73, 77–78
logical, 67, 68 Xor( ), 772
Y
Y property, 852 yield
break statement, 815 contextual keyword, 33, 814 return statement, 814 statements, multiple, 815–816
Z
Zero field, 635