Ask Question
17 December, 11:22

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
Answers (1)
  1. 17 December, 11:44
    0
    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;

    }

    }

    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
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Create a new Student class with three properties: ID, FirstName, and LastName. Override the ToString () method to display student ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers