


C# basic knowledge compilation: C# classes and structures (3)
1. What are the functional features of static classes and static members? Implement code?
Static classes and static members refer to classes or members defined using the static keyword. All members of a static class must be static members, otherwise an error will be reported. One of the characteristics of static classes and members is that they are unique. If it is a static class, it cannot be instantiated, and there is only one loaded in the memory; if it is a static variable or method, if this class can be instantiated, no matter how many times it is instantiated, there will always be only one static variable or method.
are as follows:
(1), static members
public class StatTicMember { public static string testA = string.Empty; } class Program { static void Main(string[] args) { //StaticConstruct strc = new StaticConstruct(); //StaticConstruct strcValue = new StaticConstruct(string.Empty); StatTicMember sMember1 = new StatTicMember(); StatTicMember.testA = @"静态成员"; Console.WriteLine(StatTicMember.testA); StatTicMember sMember2 = new StatTicMember(); Console.WriteLine(StatTicMember.testA); Console.ReadLine(); } }
Result:
Characteristics of static members:
a. Must be referenced through the class name, Cannot be referenced by objects of the class;
b. No matter how many times the class is instantiated, there is only the same area in the memory;
c. If variables or methods outside the method are referenced in a static method, they must also be Static, such as
public class StatTicMember { public static string testA = string.Empty; public string testB = string.Empty; public static void Method() { testA = @"my";//正确 //testB = @"my";//错误 } }
(2), static class
public static class StaticClass { public static string testA = string.Empty; public static void StaticMethod() { Console.WriteLine(@"静态方法"); } } class Program { static void Main(string[] args) { StaticClass.testA = @"静态类"; Console.WriteLine(StaticClass.testA); StaticClass.StaticMethod(); Console.ReadLine(); } }
Result:
Static class characteristics:
a. Members must also be static;
b. It cannot be instantiated. Use the class name directly to reference internal members;
c. It is a sealed class; (Note: Sealed class means that this class cannot be used as a base class, and cannot be an abstract class, that is, it cannot Derived. )
d. Cannot contain constructors.
When using static classes and members, static classes cannot be used extensively, because once it is loaded, there is an area in the memory, and it is there whether you use it or not. takes up memory. It can be used in the following situations:
a. Global variable, a variable used in the entire project, and the value cannot be changed easily, even if all modules are changed, they must respond.
b. Methods that do not operate on instance data and are not associated with specific classes in the code, such as some methods in the Math class.
2. Sealed functional features? Implement code? Why use sealed classes?
A sealed class refers to a class modified with the sealed keyword. Its purpose is to prevent derivation, that is, this class cannot be inherited.
Features:
Cannot be used as a base class, cannot be abstracted, and the call to a sealed class is faster.
public sealed class SealedClass { public string testA = string.Empty; }
3. What is an abstract class? Features? Implement code? What is the difference between interface and abstract class?
Abstract class refers to a class modified with the abstract keyword. Its function is to derive multiple classes and share the public methods and properties of the base class.
public abstract class AbstractClass { public abstract void CommonMethod(); } public class ChildClass1 : AbstractClass { public override void CommonMethod() { Console.WriteLine(@"实现公用方法1"); } } public class ChildClass2 : AbstractClass { public override void CommonMethod() { Console.WriteLine(@"实现公用方法2"); } } class Program { static void Main(string[] args) { ChildClass1 chc1 = new ChildClass1(); chc1.CommonMethod(); ChildClass2 chc2 = new ChildClass2(); chc2.CommonMethod(); Console.ReadLine(); } }
Result:
The difference between abstract class and interface:
a. A class is an abstraction of an object. Abstract classes can be understood as treating classes as Objects are abstracted into classes called abstract classes. The interface is just a specification or regulation of behavior. Microsoft's custom interface is always followed by an able field, which proves that it expresses a class "I can do...". Abstract classes are more defined in a series of closely related Between classes, most interfaces are loosely related but all implement a certain function;
b. The interface basically does not have any specific characteristics of inheritance, it only promises methods that can be called;
c , A class can implement several interfaces at a time, but can only extend one parent class;
d. Interfaces can be used to support callbacks, but inheritance does not have this feature;
e. Abstract classes cannot be sealed;
## # Similar to classes, an abstract class must also provide its own implementations for all members of the interfaces listed in the class's base class list. However, abstract classes are allowed to map interface methods to abstract methods;
h. Abstract classes implement a principle in oop, which separates the mutable and the immutable. Abstract classes and interfaces are defined as immutable, and the mutable ones are left to subclasses for implementation;
i. A good interface definition should have specific functionality, not multi-function, otherwise the interface will become pollute. If a class only implements one function of the interface and has to implement other methods in the interface, it is called interface pollution;
j. If the abstract class implements the interface, the methods in the interface can be mapped to the abstract class As an abstract method in the interface, it does not need to be implemented, but the methods in the interface are implemented in the subclass of the abstract class.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.
