!Dan Vallejo's WebLog!

Visual Studio .NET Developer

C# 2.0 Generic Interfaces

Just as in generic classes, it is useful to define generic interfaces. It's useful to define generic interfaces for collections such as linked lists.

In this example, we are defining a generic interface that requires T to be a reference type.

// File: GenericInterfaces.cs
using System;
public interface INode<T> where T:class
{
    string Value
    {
        get;
    }
    T Next
    {
        get;
    }
}
public class PersonNode : INode<PersonNode>
{
    private string name;
    private PersonNode next;
    public PersonNode(string name, PersonNode next)
    {
        this.name = name;
        this.next = next;
    }
    public string Value
    {
        get
        {
            return name;
        }
    }
    public PersonNode Next
    {
        get
        {
            return next;
        }
    }
}
public class Program
{
    static void Main()
    {
        PersonNode danNode = new PersonNode("dan", null);
        PersonNode sabetNode = new PersonNode("sabet", danNode);
        PersonNode mikeNode = new PersonNode("mike", sabetNode);
        PersonNode node = mikeNode;
        while (node != null)
        {
            Console.WriteLine(node.Value);
            node = node.Next;
        }
        Console.ReadLine();
    }
}
Published Thursday, October 14, 2004 3:39 PM by DanVallejo
Filed under:

Comments

 

blog.dreampro said:

October 14, 2004 8:15 PM
 

Amirreza Sardarzadeh said:

hi
i have a question , but not about this article

look at this line of code :

[assembly:FileIOPermission(SecurityAction.RequestMinimum , Unrestricted = true)]

when use it above class declaration the FileIOPermisson accepts only one parameter
(i know there is wrong place for use of this attribute)
ok?
i want to know what is "Unrestricted = true" , when there is only one parameter ?
how it accepts second parameter ?
i think it's irregular


thanx
bye
October 17, 2004 1:34 AM
 

Vlad Volosuk said:

The Unrestricted is a named argument.
The named arguments are declared as fields or properies.
In this example it is the propery FileIOPermissonAttribute.Unrestricted which will be assigned to true after the object creation.
October 17, 2004 7:57 PM
 

Amirreza Sardarzadeh said:

thanx for your answer
it's realy helped me
(how fast you answer)
bye and thanx again
October 18, 2004 2:43 AM
 

C# 2.0 Generic Interfaces said:

November 26, 2007 11:10 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker