[英]Multidimensional Associative Arrays in C#
Started getting into C# from JS and PHP.开始从 JS 和 PHP 进入 C#。 I am getting used to the strict use of data types and I am struggling to figure out how to declare multidimensional associative array of different data types.
我已经习惯了对数据类型的严格使用,我正在努力弄清楚如何声明不同数据类型的多维关联数组。
Like for example in PHP one would probably do something like this例如在 PHP 中,可能会做这样的事情
$roomDiscount["apartment"][0]["minDaysOfStay"] = 10;
$roomDiscount["apartment"][0]["discount"] = 0.3;
$roomDiscount["apartment"][1]["minDaysOfStay"] = 15;
$roomDiscount["apartment"][1]["discount"] = 0.35;
$roomDiscount["apartment"][2]["minDaysOfStay"] = 16;
$roomDiscount["apartment"][2]["discount"] = 0.5;
$roomDiscount["presidential suite"][0]["minDaysOfStay"] = 10;
$roomDiscount["presidential suite"][0]["discount"] = 0.1;
$roomDiscount["presidential suite"][1]["minDaysOfStay"] = 15;
$roomDiscount["presidential suite"][1]["discount"] = 0.15;
$roomDiscount["presidential suite"][2]["minDaysOfStay"] = 16;
$roomDiscount["presidential suite"][2]["discount"] = 0.2;
So far I have been struggling with Dictionaries到目前为止,我一直在努力使用字典
private static void SkiTripWithDictionariesArrays()
{
int daysOfStay = int.Parse(Console.ReadLine());
string typeOfAccomodation = Console.ReadLine().ToLower().Trim();
string review = Console.ReadLine().ToLower().Trim();
Dictionary<string, double> roomPrices = new Dictionary<string, double>();
Dictionary<string, object> roomDiscounts = new Dictionary<string, object>(); // <---- thats the bugger
Dictionary<string, double> reviewAdjustment = new Dictionary<string, double>();
//populate room prices
roomPrices.Add("room for one person", 18);
roomPrices.Add("apartment", 25);
roomPrices.Add("president apartment", 35);
}
Your current PHP code is like this:您当前的 PHP 代码如下:
$roomDiscount["apartment"][0]["minDaysOfStay"] = 10;
It is close to this kind of structure in C#:在 C# 中与这种结构很接近:
Dictionary<string, List<Dictionary<string, double>>>
Declaring such an object probably isn't a good way of going about it.声明这样一个 object 可能不是一个好方法。 Instead you should define class objects instead.
相反,您应该定义 class 对象。 The example I give below is for illustrative purposes only, and isn't necessarily the best way to do this (there are many different approaches to this situation):
我在下面给出的示例仅用于说明目的,不一定是最好的方法(这种情况有很多不同的方法):
public class RoomDiscount
{
public int MinDaysOfStay {get;set;}
public double Discount {get;set;}
}
public class RoomDiscounts
{
public List<RoomDiscount> DiscountBands {get;set;}
}
Usage:用法:
Dictionary<string, RoomDiscounts> discountDetails = new Dictionary<string, RoomDiscounts>();
discountDetails["apartment"] = new RoomDiscounts {
DiscountBands = new List<RoomDiscount> {
new RoomDiscount {
MinDaysOfStay = 10,
Discount = 0.3
},
new RoomDiscount {
MinDaysOfStay = 15,
Discount = 0.35
},
new RoomDiscount {
MinDaysOfStay = 16,
Discount = 0.5
}
}
};
string room = "apartment";
int daysOfStay = 26;
double discount = discountDetails[room].DiscountBands.OrderByDescending(b => b.MinDaysOfStay).FirstOrDefault(b => daysOfStay >= b.MinDaysOfStay)?.Discount ?? 0;
Again, this is just an example of one way you could organise your data in a more strongly-typed fashion.同样,这只是您可以以更强类型的方式组织数据的一种方式的示例。 Note that this will throw an exception if the room type isn't defined, so you could use
TryGetValue
to retrieve the details from the dictionary.请注意,如果未定义房间类型,这将引发异常,因此您可以使用
TryGetValue
从字典中检索详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.