There are 2 classes - a base class, and inherited class. I want the function print to be able print objects of both of these classes. Though the name of the function is similar in both cases, the signature of the function is different. Why then the function does not work here?
this is the code:
using System;
namespace ConsoleApp8
{
internal class Program
{
static void Main(string[] args)
{
Asset asset = new Asset(); // פולימורפיזם
asset.name = "A";
asset.price = 1;
Stock stock = new Stock();
stock.name = "B";
stock.price = 2;
stock.shared = 5;
print(stock);
print(asset);
static void print(Asset asset)
{
Console.WriteLine(asset.price + " " + asset.name);
}
static void print(Stock stock)
{
Console.WriteLine(stock.price + " " + stock.name + " " + stock.shared);
}
}
class Asset
{
public string name;
public int price;
}
class Stock : Asset
{
public int shared;
}
}
}
Thank you!
Local functions cannot be overloaded:
A method cannot contain two local functions with the same name but different parameters. That is, overloaded local functions are not supported.
So what you can do is to create two local methods with different names:
void PrintAsset(Asset asset) {}
void PrintStock(Stock stock) {}
And some more restrictions for local functions:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With