简单做网站需要学什么软件,营销培训课程ppt,哈尔滨做设计和网站的公司吗,郑州小程序开发公司文章目录 泛型基础示例1#xff1a;泛型类与方法示例2#xff1a;泛型方法示例3#xff1a;泛型约束示例4#xff1a;泛型委托与事件示例5#xff1a;泛型单例模式#xff08;Unity#xff09; 在Unity中#xff0c;C#的泛型编程是一种强大的工具#xff0c;它允许你编… 文章目录 泛型基础示例1泛型类与方法示例2泛型方法示例3泛型约束示例4泛型委托与事件示例5泛型单例模式Unity 在Unity中C#的泛型编程是一种强大的工具它允许你编写可复用且类型安全的代码。以下将详细讲解泛型并通过五个代码实例来展示其在Unity中的实际应用。 泛型基础
什么是泛型 泛型是C#中的一种特性它允许你在定义类、接口、方法或委托时指定一个或多个类型参数。这些类型参数在编译时由实际的类型替换从而创建特定类型的实例。这样可以确保数据类型的一致性并避免不必要的装箱和拆箱操作提高性能和安全性。
例如
public class MyGenericClassT
{public T Value { get; set; }
}在这个例子中T是一个类型参数当创建MyGenericClassint或MyGenericClassstring等实例时T会被具体的数据类型替代。
示例1泛型类与方法
场景一个通用容器类用于存储任意类型的数据。
// 定义泛型类
public class DataContainerT
{private T data;public void SetData(T value){data value;}public T GetData(){return data;}
}// 使用
var intContainer new DataContainerint();
intContainer.SetData(42);
int number intContainer.GetData(); // number 现在是 42var stringContainer new DataContainerstring();
stringContainer.SetData(Hello, World!);
string message stringContainer.GetData(); // message 现在是 Hello, World!示例2泛型方法
场景一个交换两个变量值的方法适用于任何引用类型或值类型。
public static class Utilities
{// 泛型方法public static void SwapT(ref T a, ref T b){T temp a;a b;b temp;}
}// 使用
int x 10, y 20;
Utilities.Swap(ref x, ref y); // 交换后x20, y10string first Alice, second Bob;
Utilities.Swap(ref first, ref second); // 交换后firstBob, secondAlice示例3泛型约束
场景一个泛型类需要限制只能处理实现了某个接口的类型。
public interface IComparableT
{int CompareTo(T other);
}public class SortableListT where T : IComparableT
{private ListT list new ListT();public void Add(T item){list.Add(item);}public void Sort(){list.Sort((a, b) a.CompareTo(b));}
}// 使用
class CustomType : IComparableCustomType
{public int CompareTo(CustomType other){// 实现比较逻辑}// ...
}SortableListCustomType customList new SortableListCustomType();
customList.Add(new CustomType());
customList.Sort();示例4泛型委托与事件
场景一个通用事件处理器接受任何类型的参数。
public delegate void GenericEventHandlerT(object sender, T args);public class EventManager
{public event GenericEventHandlerstring OnStringEvent;public event GenericEventHandlerVector3 OnVector3Event;public void RaiseStringEvent(string message){OnStringEvent?.Invoke(this, message);}public void RaiseVector3Event(Vector3 position){OnVector3Event?.Invoke(this, position);}
}// 使用
EventManager manager new EventManager();manager.OnStringEvent (sender, message) Debug.Log($String event: {message});
manager.OnVector3Event (sender, position) Debug.Log($Vector3 event: {position});manager.RaiseStringEvent(Hello);
manager.RaiseVector3Event(new Vector3(1, 2, 3));示例5泛型单例模式Unity
场景创建一个可以在Unity中重用的泛型单例类。
public class SingletonT : MonoBehaviour where T : SingletonT
{protected static T _instance;public static T Instance{get{if (_instance null){_instance FindObjectOfTypeT();if (_instance null){GameObject obj new GameObject(typeof(T).Name);_instance obj.AddComponentT();}}return _instance;}}protected virtual void Awake(){if (_instance ! this){Destroy(gameObject);return;}DontDestroyOnLoad(gameObject);}
}// 使用
public class GameManager : SingletonGameManager
{// 游戏管理器的具体实现...
}以上五个示例展示了泛型在Unity项目中的不同应用场景从简单的数据容器到复杂的事件处理和设计模式实现。通过使用泛型可以有效减少重复代码增强代码的灵活性和可扩展性。
python推荐学习汇总连接 50个开发必备的Python经典脚本(1-10)
50个开发必备的Python经典脚本(11-20)
50个开发必备的Python经典脚本(21-30)
50个开发必备的Python经典脚本(31-40)
50个开发必备的Python经典脚本(41-50) ————————————————
最后我们放松一下眼睛