Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#方法中从字典返回int值_C#_Dictionary - Fatal编程技术网

在C#方法中从字典返回int值

在C#方法中从字典返回int值,c#,dictionary,C#,Dictionary,我将枚举值设置到字典中 另一方面,我有一个获取枚举类型并返回枚举值的方法,该枚举值应该是整数 这是我的类和构造函数 private static readonly Dictionary<PointsType, int> _calculatePoints = new Dictionary<PointsType, int>(); public AddPointHelper() { _calculatePoints.Add(Points

我将枚举值设置到字典中

另一方面,我有一个获取枚举类型并返回枚举值的方法,该枚举值应该是整数

这是我的类和构造函数

       private static readonly Dictionary<PointsType, int> _calculatePoints = new Dictionary<PointsType, int>();

   public AddPointHelper()
   {
       _calculatePoints.Add(PointsType.FidiBoardPostWithPhotoAndHashtag,100);
       _calculatePoints.Add(PointsType.FidiBoardPostWithHashtag,100);
       _calculatePoints.Add(PointsType.ClubMember, 100);
       _calculatePoints.Add(PointsType.CompeteResturantReview, 100);

   }
我的错误是参数是bool。但是我想把它作为整数返回


如何从字典中获取整数值?

.ContainsKey
用于检查字典中是否存在给定的键,而不是检索分配给该键的值。如Lasse所述,使用索引器。试着做一些像:

if (_calculatePoints.ContainsKey(pointsType))
{ 
  return _calculatePoints[pointsType]; 
}  
else 
{ 
  return 0; 
}

尝试使用
[]
索引器:
\u calculatePoints[pointsType]
我尝试了,但它没有与问题中的代码整合,应该是。是否确实要
\u calculatePoints
枚举点
成为
静态的?因为这样您只能有一个
AddPointHelper
的实例;TryGetValue更好。这一点在中有很好的解释。我们真的需要一个单独的答案吗?
if (_calculatePoints.ContainsKey(pointsType))
{ 
  return _calculatePoints[pointsType]; 
}  
else 
{ 
  return 0; 
}