The use of where in C#

When I was writing the project, I saw a more interesting singleton. As shown in the figure below

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 单例模型
/// </summary>
/// <typeparam name="T">模板</typeparam>
public class MonoSingleton<T> : MonoBehaviour where T :Component
{
    private static T _instance;

    private static readonly object _lock = new object();

    public static T Instance
    {
        get
        {
            if(_instance == null)
            {
                lock (_lock)
                {
                    _instance = FindObjectOfType<T>();
                    if(_instance == null)
                    {
                        GameObject obj = new GameObject("TempObj");
                        _instance = obj.AddComponent<T>();
                    }
                }
            }
            return _instance;
        }
    }
}
What I'm most curious about here is why this paragraph is used
public class MonoSingleton<T> : MonoBehaviour where T :Component

Later, I planned to find the answer, but I forgot what it did after I hadn't used it for a long time.

2.where is the meaning of restriction in SQL statement  

select column_name ,column_name form table_name WHERE column_name operator value;

3.where refers to constraints in C#

Why use constraints? The official explanation given is:

Reasons to use constraints
Constraints specify the capabilities and expectations of type parameters. Declaring these constraints means that you can use operations and method calls of the constrained type. If a generic class or method uses any operation on a generic member other than simple assignment or calls any method not supported by System.Object, constraints must be applied to the type parameters. For example, a base class constraint tells the compiler that only objects of this type, or objects derived from this type, can be used as type parameters. With this guarantee, the compiler is able to allow methods of that type to be called in generic classes. The following code example demonstrates functionality that can be added to the GenericList<T> class (from Introduction to Generics) by applying base class constraints.

4. In order to verify, I will follow where  

where T :Component
This part is deleted and found
instance = FindObjectOfType<T>();
//提示是:类型“T”不能用作泛型类型或方法“Object.FindObjectOfType<T>()"中的类型参数”T“.
//没有从”T“到”UnityEngine.Object"装箱转换或者类型参数转换

The first reaction was a little confused, what does the official point have to do with this?

Constraints specify the capabilities and expectations of type parameters.
//在 Object 中找到了该方法
 public static T FindObjectOfType<T>() where T : Object;

Based on what I have understood so far, it should be required that the generic type T is inherited from Object before the function FindObjectOfType<T>() can be used, so I think 

Component must also inherit from Objet, so this function can be used.
//在Component 类中找到了这个 
public class Component : Object

To put it simply, let's use the official statement directly:

Constraints tell the compiler what capabilities a type parameter must have. Type parameters can be of any type without any constraints. The compiler can only assume  members of System.Object  , which is the ultimate base class of any .NET type.

Guess you like

Origin blog.csdn.net/yangchaojjyy/article/details/128677124