c# get set
using System;
public class SaleItem
{
public string Name
{ get; set; }
public decimal Price
{ get; set; }
}
class Program
{
static void Main(string[] args)
{
var item = new SaleItem{ Name = "Shoes", Price = 19.95m };
Console.WriteLine($"{item.Name}: sells for {item.Price:C2}");
}
}
// The example displays output like the following:
// Shoes: sells for $19.95
//c# property get set example
float amount {get; set;}
static float interest = 9.5f;
public float InitialAmount{
set{ if(value > 1000) Console.Write("Nope must be < 1000"); }
get{ return amount;}
}
public static float InterestRate
{
get {return interest;} //Read only ... no setter
}
public static float InterestRate2
{
// no external class can assign this value ... it's private
private set{interest= value;}
get{return interest;}
}
// Automatically
public class Customer
{
public string CustomerName { get; set; }
public double[] TotalPurchasesLastThreeDays { get; set; }
}
// ----------------------- OR ---------------------------
// Manually
public class Customer
{
private double[] totalPurchasesLastThreeDays;
public string CustomerName { get; set; }
public double[] TotalPurchasesLastThreeDays
{
get
{
return totalPurchasesLastThreeDays;
}
set
{
totalPurchasesLastThreeDays = value;
}
}
}