Pass C# class as a parameter of another class

Posted by in C# category on for Beginner level | Points: 250 | Views : 44866 red flag
Rating: 4 out of 5  
 1 vote(s)

How to pass one class as a parameter of another class

Pass C# class as a parameter of another class

In this article we will learn to pass one class as a parameter of another class. In broad sense ,we are trying to create parameterized class where we will pass another class as parameter and we will use the property of the parameter class.

This article demands little knowledge of parameterized class. If you are completely new in this concept then below paragraph is for quick understanding of same.

What is parameterized class?

In case of function we use and pass parameter to pass value. In C# we can pass parameter to a class and the class which takes parameter is called parameterized class or generic class. Try to understand below code.

class TestClass<T>
        //Create object of parameterized class.
        T obj;

This is small example of parameterized class and in place of T we can pass any object, event it may be another class. In below example we will try to implement one complete example of parameterized class. Have a look on below code.

In this above example we created two classes. The Second class is our main class which we are passing as a parameter of First class.  Here is code of Main() function.

            Second s = new Second();
   = "sourav";
            s.surname = "kayal";
            First<Second> objf = new First<Second>();

At first we are creating object of “Second” class and then setting values to properties, after that we are creating object of “First” class by passing “Second” class as object.

But the problem is that we are not able to access the property of Second class within first class. In above example After T( Type) we are giving “.” But IDE is not showing any property. If we want to use property of parameter class then we have to use “where” keyword. Try to understand below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApp
    class First<T> where T:Second
        public First(T tmp)
            Console.WriteLine("Name is : " +;
            Console.WriteLine("Surname is : " + tmp.surname);
    class Second
        public string name { get; set; }
        public string surname { get; set; }
   class Program
        static void Main(string[] args)
            Second s = new Second();
   = "sourav";
            s.surname = "kayal";
            First<Second> objf = new First<Second>(s);

Here is sample output.


In this quick article we have learned how to pass class as a parameter of another class. Hope you have learned the concept.


Page copy protected against web site content infringement by Copyscape

About the Author

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
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

Posted by: Vishnupavadi on: 11/13/2013 | Points: 25
Thank you very much. Very Good Article. Easy to understand.
Posted by: Jayakrishna on: 12/12/2013 | Points: 25
Very good Explanation.Please explain what is the necessity of doing like this..Thanks you very much
Posted by: Ravikant on: 7/3/2017 | Points: 25
Thank you Sourav, But I have many class with same parameter and i want to set property as dynamically without inherit any other class so how can i do?

Login to post response

Comment using Facebook(Author doesn't get notification)