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!
static void print(Asset asset)
appear insidestatic void Main(string[] args)
, or outside? Why? Should it be inside, or outside? Why? – DymphaToString()
onAsset
andStock
? – Alsatia