Understand generic data member and generic property in c#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 4557 red flag

How to implement generic data member and generic property in C#

Understand generic data member and generic property in C#

In this article we will learn generic data member and generic property in C#. Those are very important in generic class implementation.

Generic data member

In generic class, we can define data member as generic type. Now what is generic data member? Generic data member is type less data member of generic class. In run time we can specify data type. In below example we have implemented concept of generic data member. MyGeneric class is generic class and it takes two generic data type as argument (A and B). According to type of A and B we will create two data member called First and Second.

using System;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
using System.Data.Common;
using System.Data;
using System.Reflection;
using System.Globalization;
namespace Test1
{
    class MyGeneric<A,B>
    {
        public A First;
        public B Second;
 
        public MyGeneric(A objA, B objB)
        {
            this.First = objA;
            this.Second = objB;
        }
        public voidcShowData()
        {
            Console.Write(First);
            Console.WriteLine(Second);
        }
 
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyGeneric<int, String> obj = newcMyGeneric<int,string>(100, "sourav");
            obj.ShowData();
 
            MyGeneric<string,string> obj1 = new MyGeneric<string, string>("Sourav","Kayal");
            obj1.ShowData();
 
            Console.ReadLine();
        }
    }
}


From Main() function we are creating object of MyGeneric class passing integer and string data simultaneously. And within showData() function just we are printing value of two data member.

Generic property of C#

Generic property comes in picture when we define generic data member and want to set data member through property.  Syntax is generic property is very similar with general property. Have a look on below code to understand generic property.

Here GetSet is a generic property which is used to set and get to DataMember which is one generic data member.

using System;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
using System.Data.Common;
using System.Data;
using System.Reflection;
using System.Globalization;
namespace Test1
{
    class MyGeneric<A>
    {
        public A DataMember;
        public A GetSet
        {
            get
            {
                return this.DataMember;
            }
            set
            {
                this.DataMember = value;
            }
        }
 
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyGeneric<int> obj = new MyGeneric<int>();
obj.GetSet = 100;
            Console.WriteLine("Value from Property : " +  obj.GetSet);
            Console.ReadLine();
        }
    }
}

Here is sample output



Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)