بررسی تغییرات نحوهی تعریف خواص در C# 13
نویسنده: وحید نصیری
تاریخ: ۱۴۰۳/۰۹/۰۴ ۲۰:۲۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public static partial class ValidationHelpers
{
[GeneratedRegex("sqlserver|postgres|mysql")]
public static partial Regex IsRelationalDatabase();
}public static partial class ValidationHelpers
{
[GeneratedRegex("sqlserver|postgres|mysql")]
public static partial Regex IsRelationalDatabase { get; }
}partial class TestCollection<T> : IEnumerable<T>
{
private T[] _items;
public partial int Count { get; } // Property declaration
public partial T this[Index index] { get; set; } // Indexer declaration
// ....
}
partial class TestCollection<T>
{
public partial int Count => _items.Length; // Property implementation
public partial T this[Index index] // Indexer implementation
{
get => _items[index];
set => _items[index] = value;
}
}<PropertyGroup> <LangVersion>preview</LangVersion> </PropertyGroup>
public class Person {
public int BirthYear { get; set; }
}public class Person {
[CompilerGenerated]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int <BirthYear>k__BackingField;
public int BirthYear {
[CompilerGenerated]
get { return <BirthYear>k__BackingField; }
[CompilerGenerated]
set { <BirthYear>k__BackingField = value; }
}
}public class Example
{
private int _number; // Custom backing field
public int Number
{
get => _number; // Custom getter
set
{
if (value < 0) throw new ArgumentException("Value cannot be negative");
_number = value; // Custom setter with validation
}
}
}public class Example
{
public int Number
{
get => field; // Direct access to backing field
set
{
if (value < 0) throw new ArgumentException("Value cannot be negative");
field = value; // Assign directly to backing field
}
}
}// File1.cs
public partial class Product
{
public string Name { get; set; } // Product name
public decimal BasePrice { get; set; } // Base price of the product
public decimal Discount { get; set; } // Discount percentage
public bool IsOnSale { get; set; } // Indicates if the product is on sale
public bool IsVipCustomer { get; set; } // Indicates if the customer is VIP
public bool TaxApplicable { get; set; } // Indicates if tax is applicable
// Partial property declaration for final price calculation
public partial decimal FinalPrice { get; }
}// File2.cs
public partial class Product
{
public partial decimal FinalPrice
{
get
{
// Apply discount if the product is on sale
if (IsOnSale)
{
return BasePrice - (BasePrice * Discount / 100);
}
return BasePrice; // Return base price if no discount applies
}
}
}// File3.cs
public partial class Product
{
public partial decimal FinalPrice
{
get
{
// Apply additional discount for VIP customers
if (IsVipCustomer)
{
decimal vipDiscount = Discount + 10; // Add an extra 10% discount for VIPs
return BasePrice - (BasePrice * vipDiscount / 100);
}
return BasePrice; // Return base price if not a VIP
}
}
}// File4.cs
public partial class Product
{
public partial decimal FinalPrice
{
get
{
decimal price = BasePrice;
// Add tax if applicable
if (TaxApplicable)
{
decimal taxRate = 0.15m; // 15% tax rate
price += BasePrice * taxRate;
}
return price;
}
}
}public class Program
{
public static void Main(string[] args)
{
// Product 1: On sale with general discount
var product1 = new Product
{
Name = "Laptop",
BasePrice = 100_000_000, // 100,000,000 IRR
Discount = 10, // 10% general discount
IsOnSale = true,
IsVipCustomer = false,
TaxApplicable = false
};
Console.WriteLine($"Product 1 ({product1.Name}) Final Price: {product1.FinalPrice:N0} IRR");
// Product 2: VIP customer with additional discount
var product2 = new Product
{
Name = "Smartphone",
BasePrice = 80_000_000, // 80,000,000 IRR
Discount = 5, // 5% general discount
IsOnSale = true,
IsVipCustomer = true,
TaxApplicable = false
};
Console.WriteLine($"Product 2 ({product2.Name}) Final Price: {product2.FinalPrice:N0} IRR");
// Product 3: Product with tax applied
var product3 = new Product
{
Name = "Tablet",
BasePrice = 50_000_000, // 50,000,000 IRR
Discount = 0,
IsOnSale = false,
IsVipCustomer = false,
TaxApplicable = true
};
Console.WriteLine($"Product 3 ({product3.Name}) Final Price: {product3.FinalPrice:N0} IRR");
}
}Product 1 (Laptop) Final Price: 90,000,000 IRR Product 2 (Smartphone) Final Price: 68,000,000 IRR Product 3 (Tablet) Final Price: 54,500,000 IRR