Home > Software engineering >  C# properties and "tostring" method
C# properties and "tostring" method

Time:05-15

I was trying to understand properties better and I came across this page with this example:

https://www.tutorialspoint.com/csharp/csharp_properties.htm

using System;
namespace tutorialspoint {
class Student {
  private string code = "N.A";
  private string name = "not known";
  private int age = 0;
  
  // Declare a Code property of type string:
  public string Code {
     get {
        return code;
     }
     set {
        code = value;
     }
  }
  
  // Declare a Name property of type string:
  public string Name {
     get {
        return name;
     }
     set {
        name = value;
     }
  }
  
  // Declare a Age property of type int:
  public int Age {
     get {
        return age;
     }
     set {
        age = value;
     }
  }
  public override string ToString() {
     return "Code = "   Code  ", Name = "   Name   ", Age = "   Age;
  }
}

class ExampleDemo {
  public static void Main() {
  
     // Create a new Student object:
     Student s = new Student();
     
     // Setting code, name and the age of the student
     s.Code = "001";
     s.Name = "Zara";
     s.Age = 9;
     Console.WriteLine("Student Info: {0}", s);
     
     //let us increase age
     s.Age  = 1;
     Console.WriteLine("Student Info: {0}", s);
     Console.ReadKey();
  }
}
}

output: Student Info: Code = 001, Name = Zara, Age = 9

I don't understand how the first example is able to output the whole line written in the class "student". In the main method, we are using "s" which is an object created in the class "exampledemo". How is it able to call a method from another class? I guess it's something related to inheritance and polymorphism (I googled the override keyword) but it seems to me that the two classes are indipendent and not a subclass of the other. I'm a total beginner at programming and probably quite confused.

CodePudding user response:

s is of type Student (as declared on the first line of Main()). Therefore one can call a method on the object to modify it or print it. When you do s.Name = "Zara"; you are already calling a method on Student to update it (technically, a method and a property are the same, they only differ by syntax).

The line Console.WriteLine("Student Info: {0}", s); is actually the same as Console.WriteLine("Student Info: " s.ToString());. The compiler allows writing this in a shorter form, but internally the same thing happens.

CodePudding user response:

Let me show a real life example.

You have a sketch of your dream bicycle. Your bicycle exists only in sketch. This is a class.

Then you are going to garage and building your bicycle from the sketch. This process can be called like creation of object of bicycle at factory from your sketch.

According to your example, class is Student.

You are creating an object by the following line:

Student s = new Student(); 

Object takes space in memory. How can we read values of objects from memory? By using object reference. s is an object reference to the newly created object type of Student.

How is it able to call a method from another class?

s is an object reference to the newly created object type of Student. So it can call any public method of this object.

I was trying to understand properties

Properties are evolution of getter and setter methods. Looking for a short & simple example of getters/setters in C#

The compiler generates a pair of get and set methods for a property, plus a private backing field for an auto-implemented property. Are C# properties actually Methods?

  • Related