Created
June 11, 2020 13:13
-
-
Save vgashic/4dc0cc6fc2e21d3b9ca8d6eba77ebdcf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class Program { | |
public static void Main () { | |
Product p1 = new Product (); | |
Console.WriteLine (p1.ID); | |
Product p2 = new Product (); | |
Console.WriteLine (p2.ID); | |
Console.WriteLine (p1.ID); | |
p1.SetPrice (2000); | |
p2.SetPrice (200000); | |
} | |
public class Product { | |
private static int _counter; | |
private int _id; | |
public Product () { | |
if (_counter == null) { | |
_counter = 1; | |
} else { | |
_counter++; | |
} | |
_id = _counter; | |
} | |
public int ID { | |
get { | |
return _id; | |
} | |
} | |
private int _price; | |
public int Price { | |
get { return _price; } | |
} | |
public void SetPrice (int price) { | |
if (price >= 10 && price <= 10000) { | |
_price = price; | |
} else { | |
throw new ArgumentOutOfRangeException (nameof (price)); | |
} | |
} | |
~Product () { | |
_counter--; | |
Console.WriteLine ($"Counter decreased ({_counter.ToString()})"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment