Q1: Which members can be defined in the type?
A1: 1)Constant;2)Field;3)Instance constructor;4)Static constructor;5)Method;6)Attribute;7)Event;8)Type;9)Operator overload;10)Conversion operator overload.
Q2: What is a friend assembly, and how to create a friend assembly? What is its disadvantage?
A2:The type defined in Assembly A requires access from assembly B, but does not require access from other assembly, that is, it cannot be defined as internal or public. In this case, it can be solved by A friend assembly. When building an assembly, you can useSystem. Runtime. CompilerServicesThe name of a namespaceInternalsVisibleToTo indicate that it is another assembly of "youyuan", test it:
1) create a ClassLibrary named "FriendAssembly ".
2) delete an existing class and add a class "Test"
1: using System;
2: using System.Runtime.CompilerServices;
3:
4: [assembly:InternalsVisibleTo("FriendAssemblyTest")]
5: namespace FriendAssembly
6: {
7: class Test
8: {
9: public static void Print()
10: {
11: Console.WriteLine("Test");
12: }
13: }
14: }
3) Add a ConsoleApplication project named FriendAssemblyTest and add references to the FriendAssembly project. The Main method is as follows:
1: namespace FriendAssemblyTest
2: {
3: class Program
4: {
5: static void Main(string[] args)
6: {
7: FriendAssembly.Test.Print();
8: }
9: }
10: }
4) run the command and you can print "Test". If you comment out the code, you will find that FriendAssembly. Test cannot be accessed.
[assembly:InternalsVisibleTo("FriendAssemblyTest")]
The mutual dependence of the AU $ assembly is very high. It is best to package and release it together. If the release time is too long by mistake, it may cause compatibility problems.
Q3: What are the access modifiers and their functions?
A3: 1)Private, a member can only be accessed by defining its type or any method in the nested type;2)Protected, a member can only be accessed by defining its type or any nested type and a method of a derived type in any assembly;3)Internal, members can only be accessed by methods in the definition assembly;4)Protected internal, a member can be accessed by any method that defines its type, nested type, and derived type in any assembly;5)Public, members can be accessed by any method in any assembly.
Q4: What is the default modifier of a class, what is the default type of a class member, and what is the default modifier of an interface member?
A4:The default modifier of the class is internal; the default type of the class member is private; the default modifier of the interface member is public, but explicit designation by developers is prohibited.
Q5: Can the following code be compiled? Why?
1: class MyClass
2: {
3: public virtual void TestOne()
4: {
5: }
6:
7: protected virtual void TestTwo()
8: {
9: }
10:
11: internal virtual void TestThree()
12: {
13: }
14: }
15:
16: class MyClassTwo : MyClass
17: {
18: private override void TestOne()
19: {
20: base.TestOne();
21: }
22:
23: public override void TestTwo()
24: {
25: base.TestTwo();
26: }
27:
28: public override void TestThree()
29: {
30: base.TestThree();
31: }
32: }
A5: No. When a derived class overrides a member defined in its base class, the C # compiler requires the original member to have the same accessibility.
Q6: What is a static class?
A6:Static classes are used to encapsulate a group of methods that are not associated with any objects, such as Math and Console classes. Static classes cannot be instantiated. They cannot inherit interfaces and contain non-static members. They cannot be used as fields, method parameters, and local variables. They can only inherit from the System. Object base class.
Q7: After a static class is compiled, what is the modifier in IL?
A7: abstract sealed. For details, refer to the following:
Static class:
1: namespace StaticClass
2: {
3: static class Test
4: {
5: }
6: }
IL:
Q8: When do I use a division class, structure, and interface?
A8: 1)Used for source code control. If multiple developers write a class together, use the partial keyword to mark it;2)In a file, a class or structure is divided into different logical units.3)Code splitting. For example, if you create a winform program, you can find that there is a Form1.Designer under Form1.cs. cs file, which declares a partial class Form1, the code in this class is the automatically generated design-related code, there is a Form1 file, it declares a partial class Form1 that inherits the Form class. You can edit your own code in this class to split the code, no matter how your code is modified, the designer's code is not affected.
The Division type is fully implemented by the C # compiler,CLR knows nothing about the division type.