Answer to Question #167424 in C# for Habib

Question #167424

Create a static integer field that keeps a count across all instances(objects) of a class. Create 3 or 4 object in derived class and then check total instances.


1
Expert's answer
2021-02-28T07:04:32-0500
using System;

static class Program
{
    static void Main(string[] args)
    {
        Count a = new Count();
        Count b = new Count();
        Count c = new Count();
        Console.WriteLine(Count.count);
        Count d = new Count();
        Console.WriteLine(Count.count);
    }
}

public class Count
{
    public static int count;

    public Count()
    {
        count++;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS