Why does calling the PrintFullName() method require casting it as ((Employee)PTE).PrintFullName() ?

Hi, I have two classes where one inherits from the other. Here's the code. My question is: why do I need to cast the method PrintFullName() when calling it as ((Employee)PTE).PrintFullName()? Why can't it be cast directly like (Employee)PTE.PrintFullName()?
c#
public class Employee
{
public string? firstName;
public string? lastName;

public virtual string PrintFullName()
{
//Console.WriteLine(firstName + " " + lastName);

return firstName + " " + lastName;
}
}

public class PartTimeEmployee: Employee
{
public new string PrintFullName()
{
return firstName + " " + lastName + " " + "contractor";
}
}
c#
public class Employee
{
public string? firstName;
public string? lastName;

public virtual string PrintFullName()
{
//Console.WriteLine(firstName + " " + lastName);

return firstName + " " + lastName;
}
}

public class PartTimeEmployee: Employee
{
public new string PrintFullName()
{
return firstName + " " + lastName + " " + "contractor";
}
}
c#
PartTimeEmployee PTE = new PartTimeEmployee();
PTE.firstName = "parttime";
PTE.lastName = "Employee";
((Employee)PTE).PrintFullName();
c#
PartTimeEmployee PTE = new PartTimeEmployee();
PTE.firstName = "parttime";
PTE.lastName = "Employee";
((Employee)PTE).PrintFullName();
11 Replies
mg
mg8mo ago
Because the latter tries to cast the result of PrintFullName to Employee i.e. a string to an Employee
steven preadly
steven preadlyOP8mo ago
and string cant be converted to an employee
kurumi
kurumi8mo ago
why do u use new? you can override this method in "normal" way
steven preadly
steven preadlyOP8mo ago
so in here we are just type casting PTE that its type is PartTimeEmployee to an employee then we call the PrintFullName() method fro that object correct but if we are doing it like that (Employee)PTE.PrintFullName() this mehod is returning a string so in tha case we are convering a sring to employee hi i am just readin about the mehod hiding in c# so i read that its recommended to use the new kewordin the drived class method but it i did`t add the new i will have a warring
Severity Code Description Project File Line Suppression State
Warning CS0114 'PartTimeEmployee.PrintFullName()' hides inherited member 'Employee.PrintFullName()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword. C#ForBeginners D:\Projects\C#ForBeginners\C#ForBeginners\Program.cs 23 Active
Severity Code Description Project File Line Suppression State
Warning CS0114 'PartTimeEmployee.PrintFullName()' hides inherited member 'Employee.PrintFullName()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword. C#ForBeginners D:\Projects\C#ForBeginners\C#ForBeginners\Program.cs 23 Active
kurumi
kurumi8mo ago
you should not use new caz this hides your impl. You should avoid this
No description
kurumi
kurumi8mo ago
No description
kurumi
kurumi8mo ago
use override keyword
steven preadly
steven preadlyOP8mo ago
i agree you can use it if you have the intention to hide the base class method i didnt come to method overriding yet i know that its cleaner approch @kurumi i mean i didt studied it yet
kurumi
kurumi8mo ago
Knowing When to Use Override and New Keywords - C#
Use the new and override keywords in C# to specify how methods with the same name in a base and derived class interact.
mg
mg8mo ago
correct
steven preadly
steven preadlyOP8mo ago
thank you thank you very mush
Want results from more Discord servers?
Add your server