Sponsored Ad

Sunday, May 6, 2012

Return Object of a Class from a Function in C#

You can return a object of a class from a method. This is simple program which is returning the object of share class. You can see that the class variable value is also returning.

Return Object of a Class from a Function in C#

using System;

namespace ConsoleHub
{
    class Share
    {
        public int ShareNumber = 299;
    }
    class Programs
    {       
        static void Main(string[] args)
            {
                Share ObjS = ObjFactory(); 

                Console.WriteLine("The Return Object is: ");
               Console.WriteLine(ObjS.ToString());
            Console.WriteLine(ObjS.ShareNumber);

                Console.ReadLine();
            }

            static Share ObjFactory()
            {
                Share objShare = new Share();
                objShare.ShareNumber = 201;
                return objShare;
            }       
    }   
}

No comments:

Post a Comment

Sponsored Ad

Development Updates