Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am trying to create my own class like a list generic class provided my microsoft.

below is my code:

namespace OwnGenricClass
{
    class Program
    {
        static void Main(string[] args)
        {
            Owngeneric<int> _test = new Owngeneric<int>();
            _test.addvalue(12);
            _test.addvalue(13);
            _test.addvalue(14);
            _test.addvalue(15);
            _test.addvalue(16);
            _test.extnlenght(); //showing error
            
        }
    }

    public class Owngeneric<T>
    {
        int i = 0;

        public T[] _data = new T[1];

        public void addvalue(T value)
        {
            _data[i] = value;
            i++;
            Array.Resize(ref _data, _data.Length + 1);
        }
    }

    public static class ExtensionMethodsClass
    {
        public static int extnlenght<T>(this T[] entity)
        {            
            return entity.Count();
        }
    }
}


What I have tried:

I want to get the length of the my generic type array from the extension method.

I have created but when I am going to use it showing compile time error, Its not able to recognize on the bases of type.
Posted
Updated 10-Jul-18 0:03am

Your extension is acting on the type Owngeneric<t> so that's what needs to be your "this"

public static int extnlenght<T>(this Owngeneric<T> entity)
{
    return entity.Count; // We also need to create the Count property
}


Next your custom class needs a way of returning the count of the items it holds, so you need to implement that too;

public class Owngeneric<T>
{
    int i = 0;

    public T[] _data = new T[1];

    public void addvalue(T value)
    {
        _data[i] = value;
        i++;
        Array.Resize(ref _data, _data.Length + 1);
    }

    public int Count
    {
        get
        {
            return _data.Length;
        }
    }
}
 
Share this answer
 
Comments
Graeme_Grant 10-Jul-18 4:25am    
I was thinking the same but the question asked was about generic arrays...
F-ES Sitecore 10-Jul-18 4:40am    
_data is a "generic array", his question was about extension methods more than it was arrays :)
Have a read of this great answer: c# - Why isn't Array a generic type? - Stack Overflow[^]

You would be better off working with List<T> for collections. Here is a starter: Collections tutorial - C# local quickstarts | Microsoft Docs[^]
 
Share this answer
 
Comments
Bhanu Pratap Verma 10-Jul-18 3:56am    
Can you guide me to how to correct code above?
Graeme_Grant 10-Jul-18 4:22am    
Click on the link to the Microsoft Docs and it will answer your questions...
Hi,
The above code suggestion work for me.

Thank you for you response guys, Its really appreciated.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900