C # basic concepts for learning question 25 page 1/4

Source: Internet
Author: User

Note: Some information in this article comes from the Internet. If there is any infringement, please contact me. I will declare the reference or delete it as soon as possible!
When I first learned C #, I asked a person about the data type and the branch statement and started the project. In the past two days, I have thoroughly read the relevant basic knowledge (learning and learning) and summarized 25 questions:
1. What are the differences between static and non-static members?
2. What is the difference between const and static readonly?
3. What does extern mean?
4. What does abstract mean?
5. What is the role of the internal modifier?
6. What is the sealed modifier?
7. What is the difference between override and overload?
8. What is an index indicator?
9. What is the role of the new modifier?
10. What is the meaning of this keyword?
11. Can I use Abstract Functions to override virtual functions in the base class?
12. Can there be virtual functions in the seal class?
13. What is an attribute accessor?
14. can abstract be used with virtual? Can it be used with override?
15. What members can an interface contain?
16. What is the difference between classes and structures?
17. What problems does interface multi-inheritance bring about?
18. What is the difference between an abstract class and an interface?
19. What is an alias indicator?
20. How to manually release resources?
21. P/Invoke?
22. What is the difference between StringBuilder and String?
23. What is the meaning of explicit and implicit?
24. What is the use of params?
25. What is reflection?
The following is my reference answer (within the scope of C # Language). If it is inaccurate or not comprehensive, you are welcome to correct it!
1. What are the differences between static and non-static members?
A:
Static variables are declared using the static modifier. They are created when the class is instantiated and accessed through the class.
Variables declared without the static modifier are called non-static variables. They are created when the object is instantiated and accessed through the object.
The same static variable of all instances of a class is the same value. The same non-static variable of different instances of the same class can be different values.
Non-static members, such as non-static variables and non-static functions, cannot be used in the implementation of static functions. Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Example01
{
Class Program
{
Class Class1
{
Public static String staticStr = "Class ";
Public String notstaticStr = "Obj ";
}
Static void Main (string [] args)
{
// Static variables are accessed by class. The same static variables of all instances of this class are the same value.
Console. WriteLine ("Class1's staticStr: {0}", Class1.staticStr );
Class1 tmpObj1 = new Class1 ();
TmpObj1.notstaticStr = "tmpObj1 ";
Class1 tmpObj2 = new Class1 ();
TmpObj2.notstaticStr = "tmpObj2 ";
// Non-static variables are accessed through objects. The same non-static variables of different objects can have different values.
Console. WriteLine ("tmpObj1's notstaticStr: {0}", tmpObj1.notstaticStr );
Console. WriteLine ("tmpObj2's notstaticStr: {0}", tmpObj2.notstaticStr );
Console. ReadLine ();
}
}
}

Result:
Class1's staticStr: Class
TmpObj1's notstaticStr: tmpObj1
TmpObj2's notstaticStr: tmpObj2
2. What is the difference between const and static readonly?
A:
Const
The member declared with the const modifier is called a constant, Which is initialized during the compilation and embedded into the client program.
Static readonly
The member declared with the static readonly modifier is still a variable, but it has a usage similar to a constant: it can be accessed through a class and cannot be modified after initialization. But unlike constants, this variable is initialized at runtime.
Example:
Test class:
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Example02Lib
{
Public class Class1
{
Public const String strConst = "Const ";
Public static readonly String strStaticReadonly = "StaticReadonly ";
// Public const String strConst = "Const Changed ";
// Public static readonly String strStaticReadonly = "StaticReadonly Changed ";
}
}
Client code:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using Example02Lib;
Namespace Example02
{
Class Program
{
Static void Main (string [] args)
{
// After modifying the initial strConst value of Class1 in Example02, compile only the Example02Lib Project
// Check the newly compiled example02lib.dllbeibeibeibeiexample02.exe.pdf in the resource manager to execute example02.exe.
// Do not directly debug and run in IDE because this will recompile the entire solution !!
// You can see that the output of strConst has not changed, and the output of strStaticReadonly has changed
// Indicates that the Const variable is initialized and embedded in the client program during the compilation period, while StaticReadonly is initialized at the runtime.
Console. WriteLine ("strConst: {0}", Class1.strConst );
Console. WriteLine ("strStaticReadonly: {0}", Class1.strStaticReadonly );
Console. ReadLine ();
}
}
}
Result:
StrConst: Const
StrStaticReadonly: StaticReadonly
EXAMPLE After modification:
Test class:
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Example02Lib
{
Public class Class1
{
// Public const String strConst = "Const ";
// Public static readonly String strStaticReadonly = "StaticReadonly ";
Public const String strConst = "Const Changed ";
Public static readonly String strStaticReadonly = "StaticReadonly Changed ";
}
}
Result
StrConst: Const
StrStaticReadonly: StaticReadonly Changed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.