C# Program to Generate Register Number Automatically for 100 Students using Static Constructor

This is a C# Program to generate register number automatically for 100 students using static constructor.

Problem Description

This C# Program Generates Register Number automatically for 100 Students using Static Constructor.

Problem Solution

Here the static constructor is used to initialize static fields of a class with specific values.

Program/Source Code

Here is source code of the C# Program to Generate Register Number automatically for 100 Students using Static Constructor. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Generate Register Number automatically 
 * for 100 Students using Static Constructor
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace staticprog
{
    class sample
    {
        int regnumber;
        static int nextnum;
        static sample()
        {
            nextnum=1000;
        }
        sample()
        {
            regnumber=++nextnum;
        }
       public static void Main(string[] args)
        {
           sample s=new sample();
           Console.WriteLine("#1 : {0}",s.regnumber);
               s=new sample();
           Console.WriteLine("#2 : {0}",s.regnumber);
           s = new sample();
           Console.WriteLine("#3 : {0}", s.regnumber);
           Console.ReadLine();
       }
    }
}
Program Explanation

This C# program is used to generate register number automatically for 100 students using static constructor. The static constructor is used to initialize static fields of a class with specific values. Print the generated numbers of the students.

advertisement
advertisement
Runtime Test Cases
 
#1 : 1001
#2 : 1002
#3 : 1003

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.