C#类继承自泛型集合的例子

 更新时间:2024年08月09日 08:58:29   作者:VinciYan  
在C#中,除了泛型字典外,你还可以继承其他集合类型,本文通过实例代码主要介绍了C#类继承自泛型集合,需要的朋友可以参考下

继承自泛型字典的例子

这段代码定义了一个多层嵌套的字典结构,旨在组织和存储复杂的层级数据

using System;
using System.Threading.Tasks;
class Contract : Dictionary<string, Dictionary<string, Dictionary<string, string>>>
{
    private readonly string type = "autodesk.data:exchange.contract.dynamo-1.0.0";
    public Contract()
    {
        var contractContent = new Dictionary<string, Dictionary<string, string>>
            {
                { "contract", new Dictionary<string, string>() }
            };
        Add(type, contractContent);
    }
}
class Program
{
    static void Main()
    {
        // Create an instance of Contract
        var contract = new Contract();
        // Access the outer dictionary using the predefined type key
        var typeKey = "autodesk.data:exchange.contract.dynamo-1.0.0";
        // Check if the key exists and add data
        if (contract.TryGetValue(typeKey, out var contractContent))
        {
            // Access the inner dictionary with the key "contract"
            if (contractContent.TryGetValue("contract", out var innerDictionary))
            {
                // Add key-value pairs to the innermost dictionary
                innerDictionary["key1"] = "value1";
                innerDictionary["key2"] = "value2";
                // Retrieve and display values
                Console.WriteLine(innerDictionary["key1"]); // Outputs: value1
                Console.WriteLine(innerDictionary["key2"]); // Outputs: value2
            }
        }
    }
}
value1
value2

再看一个Dynamo项目实例,这段代码的目的是创建一个嵌套字典结构,用于存储有关二进制引用组件的属性数据。使用接口IPropertySet作为多态机制的基础。通过嵌套字典结构实现多层次数据的组织和访问。提供灵活的属性管理,适合复杂对象的属性存储场景

using System;
using System.Threading.Tasks;
class BinaryReferenceComponent : Dictionary<string, Dictionary<string, IPropertySet>>
{
    private string objectId = "autodesk.data:binary.reference.component-1.0.0";
    public string ObjectId { get { return objectId; } }
    public BinaryReferenceComponent(string binaryId)
    {
        var propertyDictionary = new Dictionary<string, IPropertySet>();
        propertyDictionary.Add("String", new StringPropertySet(binaryId));
        propertyDictionary.Add("Uint32", new IntPropertySet());
        this.Add("binary_reference", propertyDictionary);
    }
}
class StringPropertySet : IPropertySet
{
    public string Id { get; set; }
    public string Revision { get; set; }
    public StringPropertySet(string binaryId, string revision = "v0")
    {
        Id = binaryId;
        Revision = revision;
    }
}
class IntPropertySet : IPropertySet
{
    public int End { get; set; }
    public int Start { get; set; }
    public IntPropertySet(int start = 0, int end = 8710)
    {
        End = end;
        Start = start;
    }
}
interface IPropertySet { }
class Program
{
    static void Main()
    {
        // Create an instance of BinaryReferenceComponent with a binaryId
        var binaryReference = new BinaryReferenceComponent("exampleBinaryId");
        // Access ObjectId
        Console.WriteLine($"ObjectId: {binaryReference.ObjectId}");
        // Access properties of the component
        if (binaryReference.TryGetValue("binary_reference", out var propertySet))
        {
            if (propertySet.TryGetValue("String", out var stringProperty))
            {
                var stringProp = stringProperty as StringPropertySet;
                Console.WriteLine($"StringProperty Id: {stringProp.Id}, Revision: {stringProp.Revision}");
            }
            if (propertySet.TryGetValue("Uint32", out var intProperty))
            {
                var intProp = intProperty as IntPropertySet;
                Console.WriteLine($"IntProperty Start: {intProp.Start}, End: {intProp.End}");
            }
        }
    }
}
ObjectId: autodesk.data:binary.reference.component-1.0.0
StringProperty Id: exampleBinaryId, Revision: v0
IntProperty Start: 0, End: 8710

继承多种集合类型

在C#中,除了泛型字典外,你还可以继承其他集合类型,例如:

  • List:可以创建自定义列表类,但不常见,建议使用组合
  • HashSet:用于无重复元素集合,但继承并不常见
  • Queue和Stack:分别用于队列和栈的实现
  • Collection和ReadOnlyCollection:更适合继承,提供了更好的方法定制化能力

示例:继承 Collection

代码说明

  • CustomCollection<T>:

    • 继承自Collection<T>
    • 重写了InsertItem方法,添加插入前后的自定义行为
    • 定义了一个ItemAdded事件,当新项目添加时触发
  • Main函数:

    • 创建CustomCollection<string>的实例
    • 订阅ItemAdded事件,输出添加的项信息
    • 添加几个项目并显示集合中的所有项目

关键点

  • 事件处理: 使用事件机制通知项的添加
  • 自定义行为: 通过重写方法实现特定逻辑
  • 灵活性CustomCollection<T>可以用于任何类型的集合
using System;
using System.Collections.ObjectModel;
class CustomCollection<T> : Collection<T>
{
    // Event triggered when an item is added
    public event Action<T> ItemAdded;
    // Custom implementation of InsertItem
    protected override void InsertItem(int index, T item)
    {
        // Add custom behavior before inserting
        Console.WriteLine($"Inserting item at index {index}: {item}");
        base.InsertItem(index, item);
        // Trigger the event after inserting
        ItemAdded?.Invoke(item);
    }
    // Method to display all items
    public void DisplayItems()
    {
        Console.WriteLine("Current items in collection:");
        foreach (var item in this)
        {
            Console.WriteLine(item);
        }
    }
}
class Program
{
    static void Main()
    {
        // Create an instance of CustomCollection
        var collection = new CustomCollection<string>();
        // Subscribe to the ItemAdded event
        collection.ItemAdded += item => Console.WriteLine($"Item added: {item}");
        // Add items to the collection
        collection.Add("Item1");
        collection.Add("Item2");
        collection.Add("Item3");
        // Display all items in the collection
        collection.DisplayItems();
    }
}

运行结果:

Inserting item at index 0: Item1
Item added: Item1
Inserting item at index 1: Item2
Item added: Item2
Inserting item at index 2: Item3
Item added: Item3
Current items in collection:
Item1
Item2
Item3

注意

在C#中,类继承自泛型字典并不常见。以下是一些原因和建议:

原因

  • 违背封装原则:直接继承集合类可能导致外部代码直接操作集合内部结构,违背封装原则
  • 集合行为的复杂性:集合类提供的行为可能不完全适合自定义类的需求,可能需要重写大量方法。继承集合类可能引入维护复杂性
    组合优于继承:常用的设计模式是组合,即在类内部包含一个集合,而不是继承它

建议

  • 使用组合:在类中定义一个集合字段,并通过方法或属性操作它。这可以更好地控制访问和行为
    扩展方法:如果需要对集合进行特定操作,可以使用扩展方法来增强其功能,而不是继承

将前面的一个继承的例子改为使用组合,运行结果不变

using System;
using System.Collections.Generic;
interface IPropertySet { }
class StringPropertySet : IPropertySet
{
    public string Id { get; set; }
    public string Revision { get; set; }
    public StringPropertySet(string binaryId, string revision = "v0")
    {
        Id = binaryId;
        Revision = revision;
    }
}
class IntPropertySet : IPropertySet
{
    public int End { get; set; }
    public int Start { get; set; }
    public IntPropertySet(int start = 0, int end = 8710)
    {
        End = end;
        Start = start;
    }
}
class BinaryReferenceComponent
{
    private Dictionary<string, Dictionary<string, IPropertySet>> properties = new();
    public string ObjectId { get; } = "autodesk.data:binary.reference.component-1.0.0";
    public BinaryReferenceComponent(string binaryId)
    {
        var propertyDictionary = new Dictionary<string, IPropertySet>
        {
            { "String", new StringPropertySet(binaryId) },
            { "Uint32", new IntPropertySet() }
        };
        properties.Add("binary_reference", propertyDictionary);
    }
    public Dictionary<string, IPropertySet> GetProperties(string key)
    {
        properties.TryGetValue(key, out var result);
        return result;
    }
}
class Program
{
    static void Main()
    {
        // Create an instance of BinaryReferenceComponent
        var binaryReference = new BinaryReferenceComponent("exampleBinaryId");
        // Access ObjectId
        Console.WriteLine($"ObjectId: {binaryReference.ObjectId}");
        // Retrieve properties using GetProperties method
        var properties = binaryReference.GetProperties("binary_reference");
        if (properties != null)
        {
            if (properties.TryGetValue("String", out var stringProperty))
            {
                var stringProp = stringProperty as StringPropertySet;
                Console.WriteLine($"StringProperty Id: {stringProp.Id}, Revision: {stringProp.Revision}");
            }
            if (properties.TryGetValue("Uint32", out var intProperty))
            {
                var intProp = intProperty as IntPropertySet;
                Console.WriteLine($"IntProperty Start: {intProp.Start}, End: {intProp.End}");
            }
        }
        else
        {
            Console.WriteLine("No properties found for the given key.");
        }
    }
}

参考

到此这篇关于C#类继承自泛型集合的文章就介绍到这了,更多相关C#类继承自泛型集合内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 分享两种实现Winform程序的多语言支持的多种解决方案

    分享两种实现Winform程序的多语言支持的多种解决方案

    本篇文章主要介绍了分享两种实现Winform程序的多语言支持的多种解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧。
    2017-02-02
  • c#文本加密程序代码示例

    c#文本加密程序代码示例

    这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
    2013-11-11
  • C#中如何连接海康威视

    C#中如何连接海康威视

    这篇文章主要介绍了C#中如何连接海康威视问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • C#多播委托的实现示例

    C#多播委托的实现示例

    C#中的多播委托是一种特殊的委托类型,可以持有多个方法的引用,并在调用时依次执行这些方法,本文就来介绍一下C#多播委托的实现示例,感兴趣的可以了解一下
    2025-12-12
  • 使用异步方式调用同步方法(实例详解)

    使用异步方式调用同步方法(实例详解)

    .NET Framework 允许您异步调用任何方法。为此,应定义与您要调用的方法具有相同签名的委托;公共语言运行时会自动使用适当的签名为该委托定义BeginInvoke和EndInvoke方法
    2013-10-10
  • C# HttpClient超时重试机制详解

    C# HttpClient超时重试机制详解

    超时重试的实现方式可以使用循环结构,在请求发起后等待一定时间,若超时未收到响应,则再次发起请求,循环次数可以根据实际情况进行设置,一般建议不超过三次,这篇文章主要介绍了C# HttpClient超时重试,需要的朋友可以参考下
    2023-06-06
  • 使用C#代码拆分或分割PDF文件的代码示例

    使用C#代码拆分或分割PDF文件的代码示例

    在许多C#和.NET应用程序中,程序化拆分PDF文件是自动化文档管理的重要步骤,本指南详细介绍了如何使用Spire.PDF for .NET库,在C#中程序化拆分或分割PDF文件,并提供了实用的方法和清晰的代码示例,需要的朋友可以参考下
    2025-11-11
  • C#中new关键字的三种用法

    C#中new关键字的三种用法

    在C#中,new关键字具有多种不同的用途,主要包括以下三个:作为运算符,作为修饰符,作为泛型约束,文章通过代码示例将这三种用法介绍的非常详细,需要的朋友可以参考下
    2024-07-07
  • C#五类运算符使用表达式树进行操作

    C#五类运算符使用表达式树进行操作

    这篇文章介绍了C#五类运算符使用表达式树进行操作,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • C#基本语法简介

    C#基本语法简介

    本文详细讲解了C#的基本语法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04

最新评论