181k views
4 votes
Create a new Student class with three properties: ID, FirstName, and LastName. Override the ToString() method to display student information. Create two new Student objects and print their contents by calling ToString() inside a Console.WriteLine() statement.

1 Answer

2 votes

Answer:

Following are the program in c#

using System;// namespace

public class student1 // main class

{

public static void Main(string[] args) // main method

{

Student sam = new Student("45", "san", "tan");

Student ram = new Student("104", "ran", "pan");

Console.WriteLine(sam); // print the data of first object

Console.WriteLine(ram);// print the data of second object

}

}

public class Student // student class

{

public string ID ;// properties id

public string FirstName; //properties name

public string LastName; //properties LastName;

public Student(string id, string firstName, string lastName) // parametrized constructor

{

ID = id;

FirstName = firstName;

LastName = lastName;

}

public override string ToString() // overriding the method ToString

{

return "ID: " + ID + ", FirstName: " + FirstName + " LastName:" + LastName;

}

}

Step-by-step explanation:

In this program we create a class student and declared a three variable i.e ID, FirstName, and LastName .We initialize them into into constructor and create a override method ToString() which return the string .In the main method method we create a two object of student class and display them using writeline method .

Output:

ID:45, FirstName:san lastName:tan

ID:104, FirstName:ran lastName:pan

User Matt Meng
by
6.4k points