C#中class于struct的区别? property和attribute的区别?

挺拔的劲松 2007-08-27 05:59:46
各位大侠,谈一下C#中,class于struct的区别? property和attribute的区别?
本人感觉挺晕~~
...全文
380 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gui0605 2007-08-27
  • 打赏
  • 举报
回复
这些GOOGLE应该解释得很清楚了吧...
yan53125 2007-08-27
  • 打赏
  • 举报
回复
面试呐?讨论一下呗

QQ15847831
dongwei2345 2007-08-27
  • 打赏
  • 举报
回复
简单点说
class是类,引用类型,存放在托管堆,释放不会很及时
struct是结构,值类型,不由系统托管,释放很快
类是面向对象最重要的特性,继承,多态,封装等(你以后就会接触到)都是以类为单位的。
shrinerain 2007-08-27
  • 打赏
  • 举报
回复
你问strcut和class区别...我还能理解...


至于attribute和property, 它们有任何相似之处么?

YunAo2008 2007-08-27
  • 打赏
  • 举报
回复
up
zhchg6666 2007-08-27
  • 打赏
  • 举报
回复
up
ccp5780199 2007-08-27
  • 打赏
  • 举报
回复
property和attribute的区别也有人详细的讲了
地址:http://blog.csdn.net/tjvictor/archive/2007/01/25/1492913.aspx
这里就不贴出来了,虽然太长了但很详细
ccp5780199 2007-08-27
  • 打赏
  • 举报
回复
c#中结构与类的区别

类与结构的实例比较
  类与结构的差别

  如何选择结构还是类

  一.类与结构的示例比较:

  结构示例:






  public struct Person
  {

  string Name;

  int height;

  int weight

  public bool overWeight()

  {

  //implement something

  }

  }



  类示例:


  public class TestTime
  {

  int hours;

  int minutes;

  int seconds;

  public void passtime()

  {



 //implementation of behavior
  }

  }

  调用过程:


  public class Test
  {

  public static ovid Main

  {

  Person Myperson=new Person //声明结构

  TestTime Mytime=New TestTime //声明类

  }

  }



  从上面的例子中我们可以看到,类的声明和结构的声明非常类似,只是限定符后面是 struct 还是 class 的区别,而且使用时,定义新的结构和定义新的类的方法也非常类似。那么类和结构的具体区别是什么呢?

  二 .类与结构的差别

  1.值类型与引用类型

  结构是值类型:值类型在堆栈上分配地址,所有的基类型都是结构类型,例如:int 对应System.int32 结构,string 对应 system.string 结构 ,通过使用结构可以创建更多的值类型

  类是引用类型:引用类型在堆上分配地址

  堆栈的执行效率要比堆的执行效率高,可是堆栈的资源有限,不适合处理大的逻辑复杂的对象。所以结构处理作为基类型对待的小对象,而类处理某个商业逻辑

  因为结构是值类型所以结构之间的赋值可以创建新的结构,而类是引用类型,类之间的赋值只是复制引用

  注:

  1.虽然结构与类的类型不一样,可是他们的基类型都是对象(object),c#中所有类型的基类型都是object

  2.虽然结构的初始化也使用了New 操作符可是结构对象依然分配在堆栈上而不是堆上,如果不使用“新建”(new),那么在初始化所有字段之前,字段将保持未赋值状态,且对象不可用

  2.继承性

  结构:不能从另外一个结构或者类继承,本身也不能被继承,虽然结构没有明确的用sealed声明,可是结构是隐式的sealed .

  类:完全可扩展的,除非显示的声明sealed 否则类可以继承其他类和接口,自身也能被继承

  注:虽然结构不能被继承 可是结构能够继承接口,方法和类继承接口一样
例如:结构实现接口


  interface IImage
  {
  void Paint();
  }
  struct Picture : IImage
  {
  public void Paint()
  {
  // painting code goes here
  }
  private int x, y, z; // other struct members
  }



  3.内部结构:

  结构:

  没有默认的构造函数,但是可以添加构造函数

  没有析构函数

  没有 abstract 和 sealed(因为不能继承)

  不能有protected 修饰符

  可以不使用new 初始化

  在结构中初始化实例字段是错误的

  类:

  有默认的构造函数

  有析构函数

  可以使用 abstract 和 sealed

  有protected 修饰符

  必须使用new 初始化

  三.如何选择结构还是类

  讨论了结构与类的相同之处和差别之后,下面讨论如何选择使用结构还是类:

  1.堆栈的空间有限,对于大量的逻辑的对象,创建类要比创建结构好一些

  2.结构表示如点、矩形和颜色这样的轻量对象,例如,如果声明一个含有 1000 个点对象的数组,则将为引用每个对象分配附加的内存。在此情况下,结构的成本较低。

  3.在表现抽象和多级别的对象层次时,类是最好的选择

  4.大多数情况下该类型只是一些数据时,结构时最佳的选择


转自http://blog.yesky.com/Blog/os586/archive/2005/06/07/141356.html
是英文的,全面包含了c#的各方面。是非常好的一本书,绝对值得下载。 Introduction xxvii Part I: The C# Language 1 Chapter 1: .NET Architecture 3 The Relationship of C# to .NET 4 The Common Language Runtime 4 Advantages of Managed Code 4 A Closer Look at Intermediate Language 7 Support for Object Orientation and Interfaces 8 Distinct Value and Reference Types 9 Strong Data Typing 9 Error Handling with Exceptions 16 Use of Attributes 17 Assemblies 17 Private Assemblies 18 Shared Assemblies 19 Reflection 19 .NET Framework Classes 19 Namespaces 21 Creating .NET Applications Using C# 21 Creating ASP.NET Applications 21 Creating Windows Forms 24 Windows Services 24 The Role of C# in the .NET Enterprise Architecture 24 Summary 26 Chapter 2: C# Basics 29 Before We Start 30 Our First C# Program 30 The Code 30 Compiling and Running the Program 31 Contents A Closer Look 31 Variables 34 Initialization of Variables 34 Variable Scope 35 Constants 38 Predefined Data Types 39 Value Types and Reference Types 39 CTS Types 40 Predefined Value Types 41 Predefined Reference Types 44 Flow Control 47 Conditional Statements 47 Loops 51 Jump Statements 54 Enumerations 55 Arrays 57 Namespaces 58 The using Statement 59 Namespace Aliases 60 The Main() Method 61 Multiple Main() Methods 61 Passing Arguments to Main() 62 More on Compiling C# Files 63 Console I/O 65 Using Comments 67 Internal Comments Within the Source Files 67 XML Documentation 68 The C# Preprocessor Directives 70 #define and #undef 70 #if, #elif, #else, and #endif 71 #warning and #error 72 #region and #endregion 72 #line 72 C# Programming Guidelines 73 Rules for Identifiers 73 Usage Conventions 74 Summary 81 Chapter 3: Objects and Types 83 Classes and Structs 84 Class Members 85 Data Members 85 Function Members 85 xi Contents readonly Fields 99 Structs 101 Structs Are Value Types 102 Structs and Inheritance 103 Constructors for Structs 103 The Object Class 104 System.Object Methods 104 The ToString() Method 105 Summary 107 Chapter 4: Inheritance 109 Types of Inheritance 109 Implementation Versus Interface Inheritance 109 Multiple Inheritance 110 Structs and Classes 110 Implementation Inheritance 111 Virtual Methods 112 Hiding Methods 113 Calling Base Versions of Functions 114 Abstract Classes and Functions 115 Sealed Classes and Methods 115 Constructors of Derived Classes 116 Modifiers 122 Visibility Modifiers 122 Other Modifiers 123 Interfaces 123 Defining and Implementing Interfaces 125 Derived Interfaces 128 Summary 130 Chapter 5: Operators and Casts 131 Operators 131 Operator Shortcuts 133 The Ternary Operator 134 The checked and unchecked Operators 134 The is Operator 135 The as Operator 136 The sizeof Operator 136 The typeof Operator 136 Contents Operator Precedence 137 Type Safety 137 Type Conversions 138 Boxing and Unboxing 141 Comparing Objects for Equality 142 Comparing Reference Types for Equality 142 The ReferenceEquals() Method 142 The virtual Equals() Method 143 The static Equals() Method 143 Comparison Operator (==) 143 Comparing Value Types for Equality 143 Operator Overloading 144 How Operators Work 145 Operator Overloading Example: The Vector Struct 146 Which Operators Can You Overload? 153 User-Defined Casts 154 Implementing User-Defined Casts 155 Multiple Casting 161 Summary 165 Chapter 6: Delegates and Events 167 Delegates 167 Using Delegates in C# 169 SimpleDelegate Example 172 BubbleSorter Example 174 Multicast Delegates 177 Events 179 The Receiver’s View of Events 180 Generating Events 182 Summary 186 Chapter 7: Memory Management and Pointers 187 Memory Management under the Hood 187 Value Data Types 188 Reference Data Types 190 Garbage Collection 192 Freeing Unmanaged Resources 193 Destructors 193 The IDisposable Interface 195 xiii Contents Implementing IDisposable and a Destructor 196 Unsafe Code 197 Pointers 198 Pointer Example: PointerPlayaround 207 Using Pointers to Optimize Performance 212 Summary 216 Chapter 8: Strings and Regular Expressions 217 System.String 218 Building Strings 219 Format Strings 223 Regular Expressions 229 Introduction to Regular Expressions 229 The RegularExpressionsPlayaround Example 230 Displaying Results 233 Matches, Groups, and Captures 234 Summary 237 Chapter 9: Collections 239 Examining Groups of Objects 239 Array Lists 240 Collections 241 Dictionaries 245 Summary 256 Chapter 10: Reflection 257 Custom Attributes 258 Writing Custom Attributes 258 Custom Attribute Example: WhatsNewAttributes 262 Reflection 265 The System.Type Class 266 The TypeView Example 268 The Assembly Class 271 Completing the WhatsNewAttributes Sample 272 Summary 276 Contents Chapter 11: Errors and Exceptions 277 Looking into Errors and Exception Handling 277 Exception Classes 278 Catching Exceptions 280 User-Defined Exception Classes 290 Summary 297 Part II: The .NET Environment 299 Chapter 12: Visual Studio .NET 301 Working with Visual Studio .NET 2003 301 Creating a Project 304 Solutions and Projects 311 Windows Application Code 314 Reading in Visual Studio 6 Projects 314 Exploring and Coding a Project 315 Building a Project 326 Debugging 331 Other .NET Tools 334 The ASP.NET Web Matrix Project 335 WinCV 335 Summary 337 Chapter 13: Assemblies 339 What Are Assemblies? 339 The Answer to DLL Hell 340 Features of Assemblies 341 Application Domains and Assemblies 341 Assembly Structure 344 Assembly Manifests 346 Namespaces, Assemblies, and Components 346 Private and Shared Assemblies 347 Viewing Assemblies 347 Building Assemblies 348 Cross-Language Support 353 The CTS and the CLS 353 Language Independence in Action 354 CLS Requirements 364 xv Contents Global Assembly Cache 366 Native Image Generator 366 Global Assembly Cache Viewer 367 Global Assembly Cache Utility (gacutil.exe) 368 Creating Shared Assemblies 369 Shared Assembly Names 369 Creating a Shared Assembly 371 Configuration 376 Configuration Categories 376 Versioning 377 Configuring Directories 387 Summary 390 Chapter 14: .NET Security 391 Code Access Security 392 Code Groups 393 Code Access Permissions and Permissions Sets 399 Policy Levels: Machine, User, and Enterprise 403 Support for Security in the Framework 405 Demanding Permissions 406 Requesting Permissions 407 Implicit Permission 410 Denying Permissions 411 Asserting Permissions 412 Creating Code Access Permissions 414 Declarative Security 414 Role-Based Security 415 The Principal 415 Windows Principal 416 Roles 417 Declarative Role-Based Security 418 Managing Security Policy 419 The Security Configuration File 419 Managing Code Groups and Permissions 423 Turning Security On and Off 423 Resetting Security Policy 423 Creating a Code Group 423 Deleting a Code Group 424 Changing a Code Group’s Permissions 424 Creating and Applying Permissions Sets 425 Distributing Code Using a Strong Name 427 Contents Distributing Code Using Certificates 429 Managing Zones 435 Summary 437 Chapter 15: Threading 439 Threading 439 Applications with Multiple Threads 441 Manipulating Threads 441 The ThreadPlayaround Sample 444 Thread Priorities 448 Synchronization 449 Summary 453 Chapter 16: Distributed Applications with .NET Remoting 455 What Is .NET Remoting? 456 Application Types and Protocols 456 CLR Object Remoting 457 .NET Remoting Overview 457 Contexts 460 Activation 461 Attributes and Properties 461 Communication between Contexts 462 Remote Objects, Clients, and Servers 462 Remote Objects 462 A Simple Server 464 A Simple Client 465 .NET Remoting Architecture 466 Channels 466 Formatters 470 ChannelServices and RemotingConfiguration 471 Object Activation 472 Message Sinks 476 Passing Objects in Remote Methods 476 Lifetime Management 481 Miscellaneous .NET Remoting Features 484 Configuration Files 484 Hosting Applications 494 Classes, Interfaces, and SoapSuds 495 Asynchronous Remoting 498 Remoting and Events 499 Call Contexts 505 Summary 507 xvii Contents Chapter 17: Localization 509 Namespace System.Globalization 510 Unicode Issues 510 Cultures and Regions 511 Cultures in Action 516 Sorting 520 Resources 522 Creating Resource Files 522 ResGen 523 ResourceWriter 523 Using Resource Files 524 The System.Resources Namespace 527 Localization Example Using Visual Studio .NET 527 Outsourcing Translations 533 Changing the Culture Programmatically 534 Using Binary Resource Files 536 Using XML Resource Files 537 Automatic Fallback for Resources 539 Globalization and Localization with ASP.NET 539 A Custom Resource Reader 540 Creating a DatabaseResourceReader 541 Creating a DatabaseResourceSet 542 Creating a DatabaseResourceManager 543 Client Application for DatabaseResourceReader 544 Summary 544 Chapter 18: Deployment 545 Designing for Deployment 545 Deployment Options 546 Xcopy 546 Copy Project 546 Deployment Projects 546 Deployment Requirements 546 Simple Deployment 547 Xcopy 548 Xcopy and Web Applications 548 Copy Project 550 Installer Projects 551 What Is Windows Installer? 551 Creating Installers 552 Advanced Options 562 Summary 569 Contents Part III: Windows Forms 571 Chapter 19: Windows Forms 573 Creating a Windows Form Application 574 Control Class 579 Size and Location 580 Appearance 580 User Interaction 580 Windows Functionality 582 Miscellaneous Functionality 582 Class Hierarchy 582 Standard Controls and Components 584 Forms 598 Form Class 599 Multiple Document Interface (MDI) 607 Custom Controls 610 Summary 622 Chapter 20: Graphics with GDI+ 623 Understanding Drawing Principles 624 GDI and GDI+ 624 Drawing Shapes 626 Painting Shapes Using OnPaint() 629 Using the Clipping Region 630 Measuring Coordinates and Areas 632 Point and PointF 632 Size and SizeF 634 Rectangle and RectangleF 635 Region 636 A Note about Debugging 637 Drawing Scrollable Windows 638 World, Page, and Device Coordinates 644 Colors 645 Red-Green-Blue (RGB) Values 645 The Named Colors 646 Graphics Display Modes and the Safety Palette 646 The Safety Palette 647 Pens and Brushes 648 Brushes 648 Pens 649 xix Contents Drawing Shapes and Lines 650 Displaying Images 652 Issues When Manipulating Images 655 Drawing Text 655 Simple Text Example 656 Fonts and Font Families 657 Example: Enumerating Font Families 659 Editing a Text Document: The CapsEditor Sample 661 The Invalidate() Method 666 Calculating Item Sizes and Document Size 667 OnPaint() 668 Coordinate Transforms 670 Responding to User Input 671 Printing 675 Implementing Print and Print Preview 676 Summary 680 Part IV: Data 683 Chapter 21: Data Access with .NET 685 ADO.NET Overview 685 Namespaces 686 Shared Classes 686 Database-Specific Classes 687 Using Database Connections 688 Using Connections Efficiently 689 Transactions 692 Commands 693 Executing Commands 694 Calling Stored Procedures 698 Fast Data Access: The Data Reader 701 Managing Data and Relationships: The DataSet Class 704 Data Tables 704 Data Columns 705 Data Relationships 711 Data Constraints 713 XML Schemas 715 Generating Code with XSD 716 Populating a DataSet 721 Populating a DataSet Class with a Data Adapter 722 Populating a DataSet from XML 723 xx Contents Persisting DataSet Changes 723 Updating with Data Adapters 724 Writing XML Output 726 Working with ADO.NET 728 Tiered Development 728 Key Generation with SQL Server 730 Naming Conventions 732 Summary 734 Chapter 22: Viewing .NET Data 735 The DataGrid Control 735 Displaying Tabular Data 735 Data Sources 738 DataGrid Class Hierarchy 746 Data Binding 750 Simple Binding 750 Data-Binding Objects 751 Visual Studio.NET and Data Access 757 Creating a Connection 758 Selecting Data 759 Generating a DataSet 762 Updating the Data Source 763 Building a Schema 764 Other Common Requirements 770 Summary 778 Chapter 23: Manipulating XML 781 XML Standards Support in .NET 782 Introducing the System.Xml Namespace 782 Using MSXML in .NET 783 Using System.Xml Classes 786 Reading and Writing Streamed XML 786 Using the XmlTextReader Class 787 Using the XmlValidatingReader Class 791 Using the XmlTextWriter Class 794 Using the DOM in .NET 795 Using the XmlDocument Class 797 Using XPath and XSLT in .NET 802 The System.Xml.XPath Namespace 803 The System.Xml.Xsl Namespace 807 Contents XML and ADO.NET 812 Converting ADO.NET Data to XML 812 Converting XML to ADO.NET Data 820 Reading and Writing a DiffGram 822 Serializing Objects in XML 825 Serialization without Source Code Access 833 Summary 836 Chapter 24: Working with Active Directory 837 The Architecture of Active Directory 838 Features 838 Active Directory Concepts 839 Characteristics of Active Directory Data 843 Schema 843 Administration Tools for Active Directory 845 Active Directory Users and Computers 845 ADSI Edit 846 Active Directory Service Interfaces (ADSI) 847 Programming Active Directory 848 Classes in System.DirectoryServices 849 Binding 849 Getting Directory Entries 854 Object Collections 855 Cache 857 Creating New Objects 857 Updating Directory Entries 858 Accessing Native ADSI Objects 859 Searching in Active Directory 860 Searching for User Objects 864 User Interface 864 Get the Schema Naming Context 864 Get the Property Names of the User Class 866 Search for User Objects 867 Summary 869 Part V: Web Programming 871 Chapter 25: ASP.NET Pages 873 ASP.NET Introduction 874 State Management in ASP.NET 875 xxii Contents ASP.NET Web Forms 875 ASP.NET Server Controls 880 ADO.NET and Data Binding 892 Updating the Event-Booking Application 893 More on Data Binding 901 Application Configuration 906 Summary 907 Chapter 26: Web Services 909 SOAP 910 WSDL 911 Web Services 913 Exposing Web Services 913 Consuming Web Services 916 Extending the Event-Booking Example 918 The Event-Booking Web Service 919 The Event-Booking Client 922 Exchanging Data Using SOAP Headers 924 Summary 929 Chapter 27: User Controls and Custom Controls 931 User Controls 932 A Simple User Control 932 Custom Controls 939 Custom Control Project Configuration 940 Basic Custom Controls 944 Creating a Composite Custom Control 949 A Straw Poll Control 951 The Candidate Controls 953 The StrawPoll Control Builder 954 Straw Poll Style 955 The Straw Poll Control 956 Summary 962 Part VI: Interop 963 Chapter 28: COM Interoperability 965 .NET and COM 966 Metadata 966 Freeing Memory 966 xxiii Contents Interfaces 967 Method Binding 969 Data Types 969 Registration 969 Threading 969 Error Handling 971 Event Handling 972 Marshaling 972 Using a COM Component from a .NET Client 973 Creating a COM Component 973 Creating a Runtime Callable Wrapper 977 Threading Issues 980 Adding Connection Points 980 Using ActiveX Controls in Windows Forms 982 Using COM Objects from within ASP.NET 985 Using a .NET Component from a COM Client 985 COM Callable Wrapper 986 Creating a .NET Component 986 Creating a Type Library 987 COM Interop Attributes 989 COM Registration 992 Creating a COM Client 993 Adding Connection Points 995 Creating a Client with a Sink Object 996 Running Windows Forms Controls in Internet Explorer 997 Summary 998 Chapter 29: Enterprise Services 999 Overview 999 History 999 Where to Use Enterprise Services? 1000 Contexts 1001 Automatic Transactions 1001 Distributed Transactions 1001 Object Pooling 1002 Role-based Security 1002 Queued Components 1002 Loosely Coupled Events 1002 Creating a Simple COM+ Application 1003 Class ServicedComponent 1003 Application Attributes 1003 Creating the Component 1004 Contents Deployment 1005 Automatic Deployment 1005 Manual Deployment 1005 Component Services Admin Tool 1006 Client Application 1008 Transactions 1009 ACID Properties 1009 Transaction Attributes 1009 Transaction Results 1010 Sample Application 1011 Summary 1021 Part VII: Windows Base Services 1023 Chapter 30: File and Registry Operations 1025 Managing the File System 1026 .NET Classes That Represent Files and Folders 1027 The Path Class 1029 Example: A File Browser 1030 Moving, Copying, and Deleting Files 1035 Example: FilePropertiesAndMovement 1035 Reading and Writing to Files 1039 Streams 1040 Reading and Writing to Binary Files 1042 Reading and Writing to Text Files 1047 Reading and Writing to the Registry 1054 The Registry 1055 The .NET Registry Classes 1057 Example: SelfPlacingWindow 1059 Summary 1066 Chapter 31: Accessing the Internet 1067 The WebClient Class 1068 Downloading Files 1068 Basic Web Client Example 1068 Uploading Files 1070 WebRequest and WebResponse Classes 1070 Other WebRequest and WebResponse Features 1071 Displaying Output as an HTML Page 1074 The Web Request and Web Response Hierarchy 1075 xxv Contents Utility Classes 1077 URIs 1077 IP Addresses and DNS Names 1079 Lower-Level Protocols 1082 Lower-Level Classes 1083 Summary 1088 Chapter 32: Windows Services 1091 What Is a Windows Service? 1091 Windows Services Architecture 1093 Service Program 1093 Service Control Program 1095 Service Configuration Program 1095 System.ServiceProcess Namespace 1095 Creating a Windows Service 1096 A Class Library Using Sockets 1096 TcpClient Example 1100 Windows Service Project 1102 Threading and Services 1107 Service Installation 1107 Installation Program 1108 Monitoring and Controlling the Service 1113 MMC Computer Management 1114 net.exe 1114 sc.exe 1115 Visual Studio .NET Server Explorer 1116 ServiceController Class 1116 Troubleshooting 1122 Interactive Services 1123 Event Logging 1123 Performance Monitoring 1130 Power Events 1135 Summary 1135 At www.wrox.com Appendix A: Principles of Object-Oriented Programming 1137 Appendix B: C# for Visual Basic 6 Developers 1177 Appendix C: C# for Java Developers 1225 Appendix D: C# for C++ Developers 1253 Index 1307
微软内部提供的详细描述C#语言结构和使用的文档,想做高级独立资深开发的不可错误,另外,此文档为英文原文版。建议作为平时学习阅读之用 目录: Table of Contents 1. Introduction 1 1.1 Hello world 1 1.2 Program structure 2 1.3 Types and variables 4 1.4 Expressions 6 1.5 Statements 8 1.6 Classes and objects 12 1.6.1 Members 12 1.6.2 Accessibility 13 1.6.3 Type parameters 13 1.6.4 Base classes 14 1.6.5 Fields 14 1.6.6 Methods 15 1.6.6.1 Parameters 15 1.6.6.2 Method body and local variables 16 1.6.6.3 Static and instance methods 17 1.6.6.4 Virtual, override, and abstract methods 18 1.6.6.5 Method overloading 20 1.6.7 Other function members 21 1.6.7.1 Constructors 22 1.6.7.2 Properties 23 1.6.7.3 Indexers 23 1.6.7.4 Events 24 1.6.7.5 Operators 24 1.6.7.6 Destructors 25 1.7 Structs 25 1.8 Arrays 26 1.9 Interfaces 27 1.10 Enums 29 1.11 Delegates 30 1.12 Attributes 31 2. Lexical structure 33 2.1 Programs 33 2.2 Grammars 33 2.2.1 Grammar notation 33 2.2.2 Lexical grammar 34 2.2.3 Syntactic grammar 34 2.3 Lexical analysis 34 2.3.1 Line terminators 35 2.3.2 Comments 35 2.3.3 White space 37 2.4 Tokens 37 2.4.1 Unicode character escape sequences 37 2.4.2 Identifiers 38 2.4.3 Keywords 39 2.4.4 Literals 40 2.4.4.1 Boolean literals 40 2.4.4.2 Integer literals 40 2.4.4.3 Real literals 41 2.4.4.4 Character literals 42 2.4.4.5 String literals 43 2.4.4.6 The null literal 45 2.4.5 Operators and punctuators 45 2.5 Pre-processing directives 45 2.5.1 Conditional compilation symbols 46 2.5.2 Pre-processing expressions 47 2.5.3 Declaration directives 47 2.5.4 Conditional compilation directives 48 2.5.5 Diagnostic directives 51 2.5.6 Region directives 51 2.5.7 Line directives 52 2.5.8 Pragma directives 52 2.5.8.1 Pragma warning 53 3. Basic concepts 55 3.1 Application Startup 55 3.2 Application termination 56 3.3 Declarations 56 3.4 Members 58 3.4.1 Namespace members 58 3.4.2 Struct members 59 3.4.3 Enumeration members 59 3.4.4 Class members 59 3.4.5 Interface members 60 3.4.6 Array members 60 3.4.7 Delegate members 60 3.5 Member access 60 3.5.1 Declared accessibility 60 3.5.2 Accessibility domains 61 3.5.3 Protected access for instance members 63 3.5.4 Accessibility constraints 64 3.6 Signatures and overloading 65 3.7 Scopes 66 3.7.1 Name hiding 69 3.7.1.1 Hiding through nesting 69 3.7.1.2 Hiding through inheritance 70 3.8 Namespace and type names 71 3.8.1 Fully qualified names 73 3.9 Automatic memory management 73 3.10 Execution order 76 4. Types 77 4.1 Value types 77 4.1.1 The System.ValueType type 78 4.1.2 Default constructors 78 4.1.3 Struct types 79 4.1.4 Simple types 79 4.1.5 Integral types 80 4.1.6 Floating point types 81 4.1.7 The decimal type 82 4.1.8 The bool type 83 4.1.9 Enumeration types 83 4.1.10 Nullable types 83 4.2 Reference types 83 4.2.1 Class types 84 4.2.2 The object type 85 4.2.3 The dynamic type 85 4.2.4 The string type 85 4.2.5 Interface types 85 4.2.6 Array types 85 4.2.7 Delegate types 85 4.3 Boxing and unboxing 86 4.3.1 Boxing conversions 86 4.3.2 Unboxing conversions 87 4.4 Constructed types 88 4.4.1 Type arguments 89 4.4.2 Open and closed types 89 4.4.3 Bound and unbound types 89 4.4.4 Satisfying constraints 89 4.5 Type parameters 90 4.6 Expression tree types 91 4.7 The dynamic type 92 5. Variables 93 5.1 Variable categories 93 5.1.1 Static variables 93 5.1.2 Instance variables 93 5.1.2.1 Instance variables in classes 93 5.1.2.2 Instance variables in structs 94 5.1.3 Array elements 94 5.1.4 Value parameters 94 5.1.5 Reference parameters 94 5.1.6 Output parameters 94 5.1.7 Local variables 95 5.2 Default values 96 5.3 Definite assignment 96 5.3.1 Initially assigned variables 97 5.3.2 Initially unassigned variables 97 5.3.3 Precise rules for determining definite assignment 97 5.3.3.1 General rules for statements 98 5.3.3.2 Block statements, checked, and unchecked statements 98 5.3.3.3 Expression statements 98 5.3.3.4 Declaration statements 98 5.3.3.5 If statements 98 5.3.3.6 Switch statements 99 5.3.3.7 While statements 99 5.3.3.8 Do statements 99 5.3.3.9 For statements 100 5.3.3.10 Break, continue, and goto statements 100 5.3.3.11 Throw statements 100 5.3.3.12 Return statements 100 5.3.3.13 Try-catch statements 100 5.3.3.14 Try-finally statements 101 5.3.3.15 Try-catch-finally statements 101 5.3.3.16 Foreach statements 102 5.3.3.17 Using statements 102 5.3.3.18 Lock statements 102 5.3.3.19 Yield statements 103 5.3.3.20 General rules for simple expressions 103 5.3.3.21 General rules for expressions with embedded expressions 103 5.3.3.22 Invocation expressions and object creation expressions 103 5.3.3.23 Simple assignment expressions 104 5.3.3.24 && expressions 104 5.3.3.25 || expressions 105 5.3.3.26 ! expressions 106 5.3.3.27 ?? expressions 106 5.3.3.28 ?: expressions 106 5.3.3.29 Anonymous functions 107 5.4 Variable references 107 5.5 Atomicity of variable references 107 6. Conversions 109 6.1 Implicit conversions 109 6.1.1 Identity conversion 109 6.1.2 Implicit numeric conversions 110 6.1.3 Implicit enumeration conversions 110 6.1.4 Implicit nullable conversions 110 6.1.5 Null literal conversions 111 6.1.6 Implicit reference conversions 111 6.1.7 Boxing conversions 111 6.1.8 Implicit dynamic conversions 112 6.1.9 Implicit constant expression conversions 112 6.1.10 Implicit conversions involving type parameters 112 6.1.11 User-defined implicit conversions 113 6.1.12 Anonymous function conversions and method group conversions 113 6.2 Explicit conversions 113 6.2.1 Explicit numeric conversions 114 6.2.2 Explicit enumeration conversions 115 6.2.3 Explicit nullable conversions 115 6.2.4 Explicit reference conversions 116 6.2.5 Unboxing conversions 117 6.2.6 Explicit dynamic conversions 117 6.2.7 Explicit conversions involving type parameters 118 6.2.8 User-defined explicit conversions 119 6.3 Standard conversions 119 6.3.1 Standard implicit conversions 119 6.3.2 Standard explicit conversions 119 6.4 User-defined conversions 119 6.4.1 Permitted user-defined conversions 119 6.4.2 Lifted conversion operators 120 6.4.3 Evaluation of user-defined conversions 120 6.4.4 User-defined implicit conversions 121 6.4.5 User-defined explicit conversions 122 6.5 Anonymous function conversions 123 6.5.1 Evaluation of anonymous function conversions to delegate types 124 6.5.2 Evaluation of anonymous function conversions to expression tree types 124 6.5.3 Implementation example 124 6.6 Method group conversions 127 7. Expressions 131 7.1 Expression classifications 131 7.1.1 Values of expressions 132 7.2 Static and Dynamic Binding 132 7.2.1 Binding-time 133 7.2.2 Dynamic binding 133 7.2.3 Types of constituent expressions 133 7.3 Operators 134 7.3.1 Operator precedence and associativity 134 7.3.2 Operator overloading 135 7.3.3 Unary operator overload resolution 136 7.3.4 Binary operator overload resolution 137 7.3.5 Candidate user-defined operators 137 7.3.6 Numeric promotions 137 7.3.6.1 Unary numeric promotions 138 7.3.6.2 Binary numeric promotions 138 7.3.7 Lifted operators 139 7.4 Member lookup 139 7.4.1 Base types 141 7.5 Function members 141 7.5.1 Argument lists 143 7.5.1.1 Corresponding parameters 144 7.5.1.2 Run-time evaluation of argument lists 145 7.5.2 Type inference 147 7.5.2.1 The first phase 147 7.5.2.2 The second phase 148 7.5.2.3 Input types 148 7.5.2.4 Output types 148 7.5.2.5 Dependence 148 7.5.2.6 Output type inferences 148 7.5.2.7 Explicit parameter type inferences 148 7.5.2.8 Exact inferences 149 7.5.2.9 Lower-bound inferences 149 7.5.2.10 Upper-bound inferences 150 7.5.2.11 Fixing 150 7.5.2.12 Inferred return type 150 7.5.2.13 Type inference for conversion of method groups 151 7.5.2.14 Finding the best common type of a set of expressions 152 7.5.3 Overload resolution 152 7.5.3.1 Applicable function member 153 7.5.3.2 Better function member 153 7.5.3.3 Better conversion from expression 154 7.5.3.4 Better conversion from type 155 7.5.3.5 Better conversion target 155 7.5.3.6 Overloading in generic classes 155 7.5.4 Compile-time checking of dynamic overload resolution 155 7.5.5 Function member invocation 156 7.5.5.1 Invocations on boxed instances 157 7.6 Primary expressions 157 7.6.1 Literals 158 7.6.2 Simple names 158 7.6.2.1 Invariant meaning in blocks 159 7.6.3 Parenthesized expressions 160 7.6.4 Member access 161 7.6.4.1 Identical simple names and type names 162 7.6.4.2 Grammar ambiguities 163 7.6.5 Invocation expressions 164 7.6.5.1 Method invocations 164 7.6.5.2 Extension method invocations 165 7.6.5.3 Delegate invocations 168 7.6.6 Element access 168 7.6.6.1 Array access 168 7.6.6.2 Indexer access 169 7.6.7 This access 170 7.6.8 Base access 170 7.6.9 Postfix increment and decrement operators 171 7.6.10 The new operator 172 7.6.10.1 Object creation expressions 172 7.6.10.2 Object initializers 173 7.6.10.3 Collection initializers 175 7.6.10.4 Array creation expressions 176 7.6.10.5 Delegate creation expressions 178 7.6.10.6 Anonymous object creation expressions 180 7.6.11 The typeof operator 181 7.6.12 The checked and unchecked operators 183 7.6.13 Default value expressions 185 7.6.14 Anonymous method expressions 185 7.7 Unary operators 186 7.7.1 Unary plus operator 186 7.7.2 Unary minus operator 186 7.7.3 Logical negation operator 187 7.7.4 Bitwise complement operator 187 7.7.5 Prefix increment and decrement operators 187 7.7.6 Cast expressions 188 7.8 Arithmetic operators 189 7.8.1 Multiplication operator 189 7.8.2 Division operator 190 7.8.3 Remainder operator 191 7.8.4 Addition operator 192 7.8.5 Subtraction operator 194 7.9 Shift operators 195 7.10 Relational and type-testing operators 197 7.10.1 Integer comparison operators 197 7.10.2 Floating-point comparison operators 198 7.10.3 Decimal comparison operators 199 7.10.4 Boolean equality operators 199 7.10.5 Enumeration comparison operators 199 7.10.6 Reference type equality operators 199 7.10.7 String equality operators 201 7.10.8 Delegate equality operators 201 7.10.9 Equality operators and null 202 7.10.10 The is operator 202 7.10.11 The as operator 202 7.11 Logical operators 203 7.11.1 Integer logical operators 204 7.11.2 Enumeration logical operators 204 7.11.3 Boolean logical operators 204 7.11.4 Nullable boolean logical operators 204 7.12 Conditional logical operators 205 7.12.1 Boolean conditional logical operators 206 7.12.2 User-defined conditional logical operators 206 7.13 The null coalescing operator 206 7.14 Conditional operator 207 7.15 Anonymous function expressions 208 7.15.1 Anonymous function signatures 210 7.15.2 Anonymous function bodies 210 7.15.3 Overload resolution 211 7.15.4 Anonymous functions and dynamic binding 211 7.15.5 Outer variables 211 7.15.5.1 Captured outer variables 212 7.15.5.2 Instantiation of local variables 212 7.15.6 Evaluation of anonymous function expressions 214 7.16 Query expressions 215 7.16.1 Ambiguities in query expressions 216 7.16.2 Query expression translation 216 7.16.2.1 Select and groupby clauses with continuations 217 7.16.2.2 Explicit range variable types 217 7.16.2.3 Degenerate query expressions 218 7.16.2.4 From, let, where, join and orderby clauses 218 7.16.2.5 Select clauses 221 7.16.2.6 Groupby clauses 222 7.16.2.7 Transparent identifiers 222 7.16.3 The query expression pattern 223 7.17 Assignment operators 224 7.17.1 Simple assignment 225 7.17.2 Compound assignment 227 7.17.3 Event assignment 228 7.18 Expression 228 7.19 Constant expressions 228 7.20 Boolean expressions 230 8. Statements 231 8.1 End points and reachability 231 8.2 Blocks 233 8.2.1 Statement lists 233 8.3 The empty statement 234 8.4 Labeled statements 234 8.5 Declaration statements 235 8.5.1 Local variable declarations 235 8.5.2 Local constant declarations 236 8.6 Expression statements 237 8.7 Selection statements 237 8.7.1 The if statement 237 8.7.2 The switch statement 238 8.8 Iteration statements 241 8.8.1 The while statement 242 8.8.2 The do statement 242 8.8.3 The for statement 243 8.8.4 The foreach statement 244 8.9 Jump statements 246 8.9.1 The break statement 247 8.9.2 The continue statement 248 8.9.3 The goto statement 248 8.9.4 The return statement 250 8.9.5 The throw statement 250 8.10 The try statement 251 8.11 The checked and unchecked statements 254 8.12 The lock statement 254 8.13 The using statement 255 8.14 The yield statement 257 9. Namespaces 259 9.1 Compilation units 259 9.2 Namespace declarations 259 9.3 Extern aliases 260 9.4 Using directives 261 9.4.1 Using alias directives 262 9.4.2 Using namespace directives 264 9.5 Namespace members 266 9.6 Type declarations 266 9.7 Namespace alias qualifiers 267 9.7.1 Uniqueness of aliases 268 10. Classes 269 10.1 Class declarations 269 10.1.1 Class modifiers 269 10.1.1.1 Abstract classes 270 10.1.1.2 Sealed classes 270 10.1.1.3 Static classes 270 10.1.2 Partial modifier 271 10.1.3 Type parameters 271 10.1.4 Class base specification 272 10.1.4.1 Base classes 272 10.1.4.2 Interface implementations 274 10.1.5 Type parameter constraints 274 10.1.6 Class body 278 10.2 Partial types 278 10.2.1 Attributes 278 10.2.2 Modifiers 279 10.2.3 Type parameters and constraints 279 10.2.4 Base class 280 10.2.5 Base interfaces 280 10.2.6 Members 280 10.2.7 Partial methods 281 10.2.8 Name binding 283 10.3 Class members 283 10.3.1 The instance type 285 10.3.2 Members of constructed types 285 10.3.3 Inheritance 286 10.3.4 The new modifier 287 10.3.5 Access modifiers 287 10.3.6 Constituent types 287 10.3.7 Static and instance members 287 10.3.8 Nested types 288 10.3.8.1 Fully qualified name 289 10.3.8.2 Declared accessibility 289 10.3.8.3 Hiding 289 10.3.8.4 this access 290 10.3.8.5 Access to private and protected members of the containing type 290 10.3.8.6 Nested types in generic classes 291 10.3.9 Reserved member names 292 10.3.9.1 Member names reserved for properties 292 10.3.9.2 Member names reserved for events 293 10.3.9.3 Member names reserved for indexers 293 10.3.9.4 Member names reserved for destructors 293 10.4 Constants 293 10.5 Fields 295 10.5.1 Static and instance fields 296 10.5.2 Readonly fields 297 10.5.2.1 Using static readonly fields for constants 297 10.5.2.2 Versioning of constants and static readonly fields 298 10.5.3 Volatile fields 298 10.5.4 Field initialization 299 10.5.5 Variable initializers 300 10.5.5.1 Static field initialization 301 10.5.5.2 Instance field initialization 302 10.6 Methods 302 10.6.1 Method parameters 304 10.6.1.1 Value parameters 306 10.6.1.2 Reference parameters 306 10.6.1.3 Output parameters 307 10.6.1.4 Parameter arrays 308 10.6.2 Static and instance methods 310 10.6.3 Virtual methods 310 10.6.4 Override methods 312 10.6.5 Sealed methods 314 10.6.6 Abstract methods 315 10.6.7 External methods 316 10.6.8 Partial methods 317 10.6.9 Extension methods 317 10.6.10 Method body 318 10.6.11 Method overloading 318 10.7 Properties 318 10.7.1 Static and instance properties 320 10.7.2 Accessors 320 10.7.3 Automatically implemented properties 325 10.7.4 Accessibility 325 10.7.5 Virtual, sealed, override, and abstract accessors 327 10.8 Events 328 10.8.1 Field-like events 330 10.8.2 Event accessors 331 10.8.3 Static and instance events 332 10.8.4 Virtual, sealed, override, and abstract accessors 333 10.9 Indexers 333 10.9.1 Indexer overloading 336 10.10 Operators 337 10.10.1 Unary operators 338 10.10.2 Binary operators 339 10.10.3 Conversion operators 339 10.11 Instance constructors 342 10.11.1 Constructor initializers 343 10.11.2 Instance variable initializers 343 10.11.3 Constructor execution 344 10.11.4 Default constructors 345 10.11.5 Private constructors 346 10.11.6 Optional instance constructor parameters 346 10.12 Static constructors 347 10.13 Destructors 349 10.14 Iterators 350 10.14.1 Enumerator interfaces 350 10.14.2 Enumerable interfaces 351 10.14.3 Yield type 351 10.14.4 Enumerator objects 351 10.14.4.1 The MoveNext method 351 10.14.4.2 The Current property 352 10.14.4.3 The Dispose method 353 10.14.5 Enumerable objects 353 10.14.5.1 The GetEnumerator method 353 10.14.6 Implementation example 354 11. Structs 360 11.1 Struct declarations 360 11.1.1 Struct modifiers 360 11.1.2 Partial modifier 361 11.1.3 Struct interfaces 361 11.1.4 Struct body 361 11.2 Struct members 361 11.3 Class and struct differences 361 11.3.1 Value semantics 362 11.3.2 Inheritance 363 11.3.3 Assignment 363 11.3.4 Default values 363 11.3.5 Boxing and unboxing 364 11.3.6 Meaning of this 365 11.3.7 Field initializers 365 11.3.8 Constructors 366 11.3.9 Destructors 367 11.3.10 Static constructors 367 11.4 Struct examples 367 11.4.1 Database integer type 367 11.4.2 Database boolean type 369 12. Arrays 371 12.1 Array types 371 12.1.1 The System.Array type 372 12.1.2 Arrays and the generic IList interface 372 12.2 Array creation 372 12.3 Array element access 373 12.4 Array members 373 12.5 Array covariance 373 12.6 Array initializers 373 13. Interfaces 377 13.1 Interface declarations 377 13.1.1 Interface modifiers 377 13.1.2 Partial modifier 377 13.1.3 Variant type parameter lists 378 13.1.3.1 Variance safety 378 13.1.3.2 Variance conversion 379 13.1.4 Base interfaces 379 13.1.5 Interface body 380 13.2 Interface members 380 13.2.1 Interface methods 381 13.2.2 Interface properties 381 13.2.3 Interface events 382 13.2.4 Interface indexers 382 13.2.5 Interface member access 382 13.3 Fully qualified interface member names 384 13.4 Interface implementations 384 13.4.1 Explicit interface member implementations 385 13.4.2 Uniqueness of implemented interfaces 387 13.4.3 Implementation of generic methods 388 13.4.4 Interface mapping 389 13.4.5 Interface implementation inheritance 392 13.4.6 Interface re-implementation 393 13.4.7 Abstract classes and interfaces 394 14. Enums 397 14.1 Enum declarations 397 14.2 Enum modifiers 397 14.3 Enum members 398 14.4 The System.Enum type 400 14.5 Enum values and operations 400 15. Delegates 401 15.1 Delegate declarations 401 15.2 Delegate compatibility 403 15.3 Delegate instantiation 403 15.4 Delegate invocation 404 16. Exceptions 407 16.1 Causes of exceptions 407 16.2 The System.Exception class 407 16.3 How exceptions are handled 407 16.4 Common Exception Classes 408 17. Attributes 409 17.1 Attribute classes 409 17.1.1 Attribute usage 409 17.1.2 Positional and named parameters 410 17.1.3 Attribute parameter types 411 17.2 Attribute specification 411 17.3 Attribute instances 416 17.3.1 Compilation of an attribute 416 17.3.2 Run-time retrieval of an attribute instance 417 17.4 Reserved attributes 417 17.4.1 The AttributeUsage attribute 417 17.4.2 The Conditional attribute 418 17.4.2.1 Conditional methods 418 17.4.2.2 Conditional attribute classes 420 17.4.3 The Obsolete attribute 421 17.5 Attributes for Interoperation 422 17.5.1 Interoperation with COM and Win32 components 422 17.5.2 Interoperation with other .NET languages 423 17.5.2.1 The IndexerName attribute 423 18. Unsafe code 425 18.1 Unsafe contexts 425 18.2 Pointer types 427 18.3 Fixed and moveable variables 430 18.4 Pointer conversions 430 18.4.1 Pointer arrays 431 18.5 Pointers in expressions 432 18.5.1 Pointer indirection 433 18.5.2 Pointer member access 433 18.5.3 Pointer element access 434 18.5.4 The address-of operator 434 18.5.5 Pointer increment and decrement 435 18.5.6 Pointer arithmetic 435 18.5.7 Pointer comparison 436 18.5.8 The sizeof operator 437 18.6 The fixed statement 437 18.7 Fixed size buffers 441 18.7.1 Fixed size buffer declarations 441 18.7.2 Fixed size buffers in expressions 442 18.7.3 Definite assignment checking 443 18.8 Stack allocation 443 18.9 Dynamic memory allocation 444 A. Documentation comments 447 A.1 Introduction 447 A.2 Recommended tags 448 A.2.1 449 A.2.2 449 A.2.3 450 A.2.4 450 A.2.5 451 A.2.6 451 A.2.7 452 A.2.8 453 A.2.9 453 A.2.10 453 A.2.11 454 A.2.12 454 A.2.13 455 A.2.14 455 A.2.15 455 A.2.16 456 A.2.17 456 A.2.18 456 A.3 Processing the documentation file 457 A.3.1 ID string format 457 A.3.2 ID string examples 458 A.4 An example 462 A.4.1 C# source code 462 A.4.2 Resulting XML 464 B. Grammar 468 B.1 Lexical grammar 468 B.1.1 Line terminators 468 B.1.2 Comments 468 B.1.3 White space 469 B.1.4 Tokens 469 B.1.5 Unicode character escape sequences 469 B.1.6 Identifiers 469 B.1.7 Keywords 470 B.1.8 Literals 471 B.1.9 Operators and punctuators 473 B.1.10 Pre-processing directives 473 B.2 Syntactic grammar 475 B.2.1 Basic concepts 475 B.2.2 Types 475 B.2.3 Variables 477 B.2.4 Expressions 477 B.2.5 Statements 484 B.2.6 Namespaces 487 B.2.7 Classes 488 B.2.8 Structs 495 B.2.9 Arrays 496 B.2.10 Interfaces 496 B.2.11 Enums 497 B.2.12 Delegates 498 B.2.13 Attributes 498 B.3 Grammar extensions for unsafe code 500 C. References 503

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧