In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
C# grammar in the application of generics, many novices are not very clear, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Generics are the syntax already provided by the .NET work 2.0 class library, which is mainly used to improve code reusability, type safety, and efficiency.
Definition of generics
A normal class and a generic class are defined below, and we can clearly see that the biggest difference between a generic class and a normal class is that there is one more.
So, this is marked, and this class is generic. Among them, this T can also be written as Ameng, Bjorn, Cpene, D, or other characters.
Public class Generic {public String Name;} public class Generic {public T Name;}
Generics, as the name implies, are types that generally refer to. For example, men, women, whites and blacks can be generally referred to as [people].
But a type can only be one type. So what is the relationship between generics and types?
In fact, it is very simple, when generics are defined, they generally refer to types; when they are used, they need to be specified which type to use.
That is, when used, it no longer refers to a general type, but to a specific type.
For example, when you define, you define a person. However, when using it, it must be clearly specified whether it is black or white.
The use of generics
Generic classes are used in the same way as ordinary classes, where objects are instantiated and internal properties or methods are called.
The following code instantiates the generic Generic and, when instantiated, specifies that the specified type of the generic Generic is String.
So to assign a value to the attribute Name of the generic Generic, you need to assign a value of type string.
Public static void Excute () {Generic gs = new Generic (); gs.Name = "Kiba518";}
The following code defines a generic Generic of type Int.
Public static void Excute () {Generic gs = new Generic (); gs.Name = 518;}
Default values for generics
The default value for generics, as shown in the following code. You need to use default (T) to assign values.
Whether the generic type is String,int,bool or a Class type, it can be assigned automatically.
Public static void Excute () {Generic gs = new Generic (); gs.Name = 518; Generic gsTask = new Generic (); gsTask.Name = new Task (() = > {Console.WriteLine ("Kiba518");});} public class Generic {public T Name = default (T);}
Constraints of generics
In generic classes, there is a special constraint for us to use.
When we do not display the declaration, this constraint does not exist. But when we display the declaration, this constraint is enforced.
Next, let's take a look at this particular constraint.
Public static void Excute () {Generic gFanXing = new Generic (); Generic
< >GFanXing = new Generic
< >(); / / Generic gs = new Generic (); this definition will cause an error} public class Generic where T: {public T Name = default (T);} public class {public string Name {get; set;}} public class FanXing: {public new string Name {get; set;}}
As shown in the code above, [where T:] is this particular constraint.
When the constraint is declared, the definition restricts the types of generics.
What are types that restrict generics?
Quite simply, generic T refers to a certain type in general. When we define a generic class, we also need to display the specified type, which is subject to this restriction.
This restriction refers to [where T:].
Its limitation is that the type T we specify must be, or it inherits from, such as the FanXing class.
Functions of generics
In C #, generics can be used not only for classes, but also directly for functions.
The specific usage is as follows:
Public static void Excute () {GenericFunc gf = new GenericFunc (); gf.FanXingFunc (new FanXing () {Name= "Kiba518"});} public class GenericFunc {public void FanXingFunc (T obj) {Console.WriteLine (obj.GetType ());}}
Quite simply, when calling a generic function, you can specify the [specified type] of the generic function.
However, here we find a problem, that is, in generic functions, when using generic objects, we find that objects are typed.
What if we want to use properties and methods in generic objects?
It's also very simple, reflection is fine.
Let's add a reflection function, GetPropertyValue, which is specifically used to get properties.
Public class GenericFunc {public void FanXingFunc (T obj) {var name = GetPropertyValue (obj, "Name"); Console.WriteLine (name);} public GetPropertyValue (obj, string name) {drv1 = obj.GetType (). GetProperty (name) .GetValue (obj, null); return drv1;}}
The output is as follows:
In this way, we get the result we want, and if we want to use the function in the generic class, the same is true, we just need to call it with reflection.
Conclusion
See here, some students may think that generics are very complex, even using the properties under its objects, have to reflect, too cumbersome, might as well not use it.
If you have such an idea, just think about it in your heart. If you say so to the old driver, he will certainly smile silently in his heart and then say to you, you are right.
And then, you don't have it.
The application of generics, as mentioned at the beginning, is mainly used to improve code reusability, type safety, and efficiency.
If you just define a class and call a property, the existence of generics is chicken ribs.
But in fact, our system is always more complex, more complex, more complex. So generics have the opportunity to show their talents.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.