Ask Question
18 September, 18:35

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.

+3
Answers (1)
  1. 18 September, 22:13
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers