C# program to get the current generation number of an object (GC.GetGeneration() Method)

C# GC.GetGeneration() Method: Here, we are going to learn how to get the current generation number of an object in C#.Net?
Submitted by Nidhi, on May 23, 2021

The GetGeneration() method of the GC class is used to get the current generation number of an object (given object).

Syntax:

    int GC.GetGeneration(object obj);

Parameter(s):

  • ob: An object whose generation information we want to get.

Return value:

It returns an integer value that represents the generation number of the specified object.

Program:

The source code to get the current generation number of an object is given below. The given program is compiled and executed successfully.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program pObj1 = new Program();
            Program pObj2 = new Program();

            Console.WriteLine("The generation of pObj1: " + GC.GetGeneration(pObj1));
            Console.WriteLine("The generation of pObj2: " + GC.GetGeneration(pObj2));

            Console.WriteLine();
        }
    }
}

Output:

The generation of pObj1: 0
The generation of pObj2: 0

Press any key to continue . . .

C# Garbage Collection (GC Class) Programs »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.