0% found this document useful (0 votes)
6 views

using System

The document contains a C# program that defines a 'Student' class with static and instance methods to manage student information. It includes constructors for creating student objects and methods for setting and getting student details like ID, name, and grade. The program also tracks the number of student instances created using a static variable 'numStudents'.

Uploaded by

nathan fitness
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

using System

The document contains a C# program that defines a 'Student' class with static and instance methods to manage student information. It includes constructors for creating student objects and methods for setting and getting student details like ID, name, and grade. The program also tracks the number of student instances created using a static variable 'numStudents'.

Uploaded by

nathan fitness
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

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

namespace static_method_practice
{
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student(); //Creates my first object of student
//Student.CountStudents(); //calling the static method

stu1.SetID(12345);
stu1.SetName("Patrick Crampton");
stu1.setgrade("B");

Student stu2 = new Student(123456);


Console.WriteLine("stu2 has been init with ID {0}",stu2.GetID());

//this will display and call the 3rd students info

Student stu3 = new Student(23456,"jim bob", "c");


Console.WriteLine("\nStu3 details \nID: {0}\nName: {1}\nGrade:
{2}", stu3.GetID(), stu3.GetName(), stu3.GetGrade());

Console.WriteLine("number of Students : {0}",


Student.numStudents );//calling the static method

Console.WriteLine("Stu1 details \nID: {0}\nName: {1}\nGrade:


{2}",stu1.GetID(),stu1.GetName(),stu1.GetGrade());//calling the non static
method

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

namespace static_method_practice
{
class Student
{
public static int numStudents;
//instance variables
private int stuID;
private string name;
private string grade;
public static void CountStudents()
{
numStudents++;

//paramertaless constructor
public Student()
{
Console.WriteLine("a New student object has been created");
numStudents++;
}
//paramater constructor
public Student(int id)
{
stuID = id;
Console.WriteLine("a New student object has been created");
numStudents++;
}

//add a third Parameter cosnrucytor which has 3 parameters - init all


3
public Student(int id, string nm,string gd)
{
stuID = id;
name = nm;
grade = gd;

Console.WriteLine("a New student object has been created");


numStudents++;

//methods
public void SetID(int id)
{
stuID = id;
}

public int GetID()


{
return stuID;

}
public void SetName(string nm)
{
name = nm;
}
public string GetName()
{
return name;
}
public void setgrade(string gd)
{
grade = gd;
}
public string GetGrade()
{
return grade;
}

}
}

You might also like