Creating a generic class in dot net
I am well aware about the concept of object oriented programming , right now am planning to develop applications on .NET languages , though I am still on the learning phase of developing applications on Microsoft Visual Studio , I want to learn web based applications , but there was a stage in the program that needed to create a generic class , can anyone explain me a bit about the generic class and the generic types.
Re: Creating a generic class in dot net
Generic class are a bit different from a non generic class , there is an advantage of using this generic class , once I was too coding and that required inclusion of declaring a property in my program , but fortunately I did not require to set the values of properties , it was done in the constructor of the generic class .
using System;
using System.Collections.Generic;
using System.Text;
public class GenericList
Code:
{
private object[] elements;
private int cnt;
public GenList()
{
elmnts = new object[10];
}
public object this[int index]
{
get { return elmnts[index]; }
set { elements[index] = val; }
}
public void Add (object param)
{
if (cnt == elmnts.Length)
{
// Increase the
object[] tempArr = null ;
elmnts.CopyTo(tempArr,0);
elmnts = new object[cnt * 2];
elmnts = tempArr;
}
elmnts[cnt] = param;
cnt = cnt + +;
}
}
Re: Creating a generic class in dot net
Generics are the most influential feature of C# 2.0. It permits defining type-safe data structures, without committing to real data types. In C# 1.0 we can also declare reference type or there is also an value type. But in the majority of the application we go through situation where we require type that can hold both reference & value type. In such circumstances we make use of generic types.
Re: Creating a generic class in dot net
Generics are the most influential feature of C# 2.0. It permits defining type-safe data structures, without committing to real data types. In C# 1.0 we can also declare reference type or there is also an value type. But in the majority of the application we go through situation where we require type that can hold both reference & value type. In such circumstances we make use of generic types.
Re: Creating a generic class in dot net
I can tell you a bit about generic methods , What remains is to describe all other characteristics in terms of the parameterized type as required. This may comprise properties, methods, and events. For instance, everywhere we can take an example , if in any program anything is repeatredmultiple occasions we can use this feature, suppose Customer appears, we make use of a parameter T in its place . We create custom list The completed generic List class is shown in Listing 4.To test the custom list, we comment out the using statement for the System.Collections.Genericnamespace and make use of List<T> if we have made use of any list .It's worth to make a note that .NET's List<T> is completely evolved and includes many more features than our sample, and makes and we can see in its programs how it makes easy the mechanics are for defining custom generic classes.