English 中文(简体)
C# Shallowplica Dictionary?
原标题:C# Shallow copy Dictionary?

我需要把字典复制成正文。

例如:

Dictionary<int,int> flags = new Dictionary<int,int>();
flags[1] = 2;
flags[2] = 3;
flags[0] = 9001;
Dictionary<int,int> flagsn = flags.MemberwiseClone();

不幸的是,这一错误又回来:“error CS1540: Cannot access Protect member object. MemberswiseClone () through a lifier of categorySystem. 收集。 通用语。 限定词必须是“点火或从中衍生出来的”类型。

不完全确定这意味着什么...... 是否有另一种方式来复制上述法规文本?

最佳回答

仅限使用<代码>的构件。 Dictionary<TKey, TValue>,因为它采用了。 IEvidable<KeyValuePair<TKey,TValue>>。 它将在新的事例中增加这一收集。

Dictionary<int, int> flagsn = new Dictionary<int, int>(flags);
问题回答

这是我所发现的一种通用方式,即你不必明确写任何类型的字,而我宁愿以可维持的理由:

var ShallowCopy = OriginalDictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签