By 22/07/2015 9 Comments

Return An Object In C#

  • Return an object in C# is possible through a method which has an object as its return type.
  • In real world programming we can see several methods which returns an object.

Example:

using System;
namespace csharpBasic
{
    class MarkSheet
    {

   // Instance fields declaration.
        public string StudentName;
        public double AspMarks;

   // Parametrized constructor declaration.
        public MarkSheet(string studentName, double aspMarks)
        {
            StudentName = studentName;
            AspMarks = aspMarks;
        }

   // A return type method returns student name.
        public string DisplayStudentName()
        {
            return StudentName;
        }

   // A return type method returns asp.net obtain marks.
        public double DisplayObtainAspMarks()
        {

            return AspMarks;
        }

   /* A parametrized return type method which has its 
      return type as a class type or an object of that class type, 
      passing 2 parameters. */
        public MarkSheet markSheetTwo(string studentName, double aspMarks)
        {

  // Note the following statement which creates and returns the second object of a class MarkSheet.
            return new MarkSheet(studentName, aspMarks);
        }
    }

    class Program
    {

        // Static main method void type declaration.
        static void Main(string[] args)
        {
        // Following statement create an object by calling its constructor.
            MarkSheet markSheetOne = new MarkSheet("ABC", 80);
            Console.WriteLine("Information of Marksheet 1:");

       // Following statement calls a method DisplayStudentName().
            Console.WriteLine("\tStudent name: {0}", markSheetOne.DisplayStudentName());

       // Following statement calls a method DisplayObtainAspMarks()
            Console.WriteLine("\tObtain marks in Asp.net: {0}", markSheetOne.DisplayObtainAspMarks());
            Console.WriteLine();

       /* Following statement creates second object of Mark Sheet class 
          by calling markSheetTwo() method and assign it to second 
          reference variable. */

            MarkSheet markSheetTwo = markSheetOne.markSheetTwo("XYZ", 60);
            Console.WriteLine("Information of Marksheet 2:");
            Console.WriteLine("\tStudent name: {0}", markSheetTwo.DisplayStudentName());
            Console.WriteLine("\tObtain marks in Asp.net: {0}", markSheetTwo.DisplayObtainAspMarks());
           
            Console.ReadKey();
        }
        /*
        The Output will be:
         
        Information of Marksheet 1:
                Student name: ABC
                Obtain marks in Asp.net: 80

         Information of Marksheet 2:
                Student name: XYZ
                Obtain marks in Asp.net: 60
       */
    }
}






← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .