Tag: enumeration

为什么我们不能在枚举其键时更改字典的值?

class Program { static void Main(string[] args) { var dictionary = new Dictionary() { {“1”, 1}, {“2”, 2}, {“3”, 3} }; foreach (var s in dictionary.Keys) { // Throws the “Collection was modified exception…” on the next iteration // What’s up with that? dictionary[s] = 1; } } } 我完全理解为什么在枚举列表时抛出此exception – 在枚举期间,枚举对象的结构不会改变似乎是合理的。 但是,更改字典的值会改变其结构吗? 具体来说,其键的结构?

C# – 播放文件夹中的随机声音文件

我正在尝试创建一个Oracle(阅读:Magic 8 Ball)。 其背后的想法是,在每个按钮按下时,播放具有明智单词的声音文件(随机选取)。 我有它使用开关工作,但我正在寻找一种方法,使它更合乎逻辑。 这是当前的样子,开关一直打开: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _8ball { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Random rnd = new Random(Guid.NewGuid().GetHashCode()); int choices = rnd.Next(0, […]

什么是线程安全(C#)? (字符串,数组,……?)

我对C#很新,所以请耐心等待。 我对线程安全有点困惑。 什么时候是线程安全的东西什么时候不安全? 从字段中读取 (只读取之前初始化的内容)总是线程安全吗? //EXAMPLE RSACryptoServiceProvider rsa = new RSACrytoServiceProvider(); rsa.FromXmlString(xmlString); //Is this thread safe if xml String is predifined //and this code can be called from multiple threads? 从数组或列表访问对象始终是线程安全的(如果您使用for循环进行枚举)? //EXAMPLE (a is local to thread, array and list are global) int a = 0; for(int i=0; i<10; i++) { a += array[i]; a […]

枚举Outlook ContactItem属性

我正在尝试使用以下代码枚举Microsoft.Office.Interop.Outlook.ContactItem对象的属性(让我们称之为ci): System.Reflection.BindingFlags bf = System.Reflection.BindingFlags.Default; foreach (System.Reflection.PropertyInfo pi in ci.GetType().GetProperties(bf)) { Console.WriteLine(“Property Info {0}”, pi.Name); } 我实际上尝试了几种BindingFlag值的组合,但是没有返回任何属性。 这是ContactItem的定义方式:使用System.Runtime.InteropServices; namespace Microsoft.Office.Interop.Outlook { [Guid(“00063021-0000-0000-C000-000000000046”)] [CoClass(typeof(ContactItemClass))] public interface ContactItem : _ContactItem, ItemEvents_10_Event { } } 这就是_ContactItem的定义方式(为简单起见,我只保留了3个道具): using System; using System.Runtime.InteropServices; namespace Microsoft.Office.Interop.Outlook { [TypeLibType(4160)] [Guid(“00063021-0000-0000-C000-000000000046”)] public interface _ContactItem { [DispId(14848)] string Account { get; set; } [DispId(63511)] Actions […]