Skip to content

Instantly share code, notes, and snippets.

@vgashic
Created June 11, 2020 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgashic/4dc0cc6fc2e21d3b9ca8d6eba77ebdcf to your computer and use it in GitHub Desktop.
Save vgashic/4dc0cc6fc2e21d3b9ca8d6eba77ebdcf to your computer and use it in GitHub Desktop.
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