base-class-library

What is C# analog of C++ std::pair?

和自甴很熟 提交于 2019-12-28 01:41:33
问题 I'm interested: What is C#'s analog of std::pair in C++? I found System.Web.UI.Pair class, but I'd prefer something template-based. Thank you! 回答1: Tuples are available since .NET4.0 and support generics: Tuple<string, int> t = new Tuple<string, int>("Hello", 4); In previous versions you can use System.Collections.Generic.KeyValuePair<K, V> or a solution like the following: public class Pair<T, U> { public Pair() { } public Pair(T first, U second) { this.First = first; this.Second = second; }

Can a byte[] buffer for a MemoryStream have variable size?

a 夏天 提交于 2019-12-22 11:21:37
问题 I'm serializing an object to a byte[] using MemoryStream : byte[] serialized = new byte[1000]; using (MemoryStream stream = new MemoryStream(serialized)) using (TextWriter textWriter = new StreamWriter(stream)) serializer.Serialize(textWriter, stuffToSerialize); is there any way I can set 'serialized' to grow according to the size of the stuffToSerialize ? 回答1: The parameterless constructor new MemoryStream() uses one. Then serialise into it, and then when you need the byte[] call ToArray()

Adding Assemblies to BuildManager using CodeDOM causing intermittent errors

老子叫甜甜 提交于 2019-12-21 19:47:42
问题 I am using CodeDOM to create an in-memory assembly at run time like so: public Assembly Compile(CodeCompileUnit targetUnit) { string path = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath); var compilerParameters = new CompilerParameters { GenerateInMemory = true, IncludeDebugInformation = true, TreatWarningsAsErrors = true, WarningLevel = 4, CompilerOptions = "/nostdlib", }; //For system.dll compilerParameters.ReferencedAssemblies.Add(typeof(System

Is there any kind of “ReferenceComparer” in .NET?

拟墨画扇 提交于 2019-12-21 06:59:08
问题 There are several places in BCL where one can make use of IEqualityComparer. Like Enumerable.Contains or Dictionary Constructor. I can provide my comparer if I'm not happy with the default one. Sometimes I want to know whether the collection contains that very object that I have reference to. Not the one that is considered "equal" in any other meaning. The question is: whether there exists standard equality comparer in the BCL that relies only on ReferenceEquals method? The one that I wrote

The current status of System.Net.Http vs. Microsoft.Net.Http

那年仲夏 提交于 2019-12-21 03:08:31
问题 I am confused with packaging of HttpClient . Earlier it was distributed as a part of Microsoft.Http.Net NuGet package while System.Net.Http was considered legacy. Looks like now it's the opposite: there is a fresh System.Net.Http package for all platforms and Microsoft.Net.Http has not been updated in a while and according to folks at Microsoft development team is going to be deprecated. Questions then: Can we replace dependencies on Microsoft.Net.Http NuGet package with (the newest) System

CallerMemberName in .NET 4.0 not working

别来无恙 提交于 2019-12-20 11:21:50
问题 I am trying to use CallerMemberName attribute in .NET 4.0 via BCL portability pack. It is always returning an empty string instead of the member name. What am I doing wrong? public partial class Form1 : Form { public Form1() { InitializeComponent(); MessageBox.Show(new class2().CallMe); } } public class class2 { public string CallMe { get { return HelpMe(); } } private string HelpMe([CallerMemberName] string param = "") { return param; } } 回答1: Targeting 4.0 works just fine if you add:

CallerMemberName in .NET 4.0 not working

时光怂恿深爱的人放手 提交于 2019-12-20 11:20:48
问题 I am trying to use CallerMemberName attribute in .NET 4.0 via BCL portability pack. It is always returning an empty string instead of the member name. What am I doing wrong? public partial class Form1 : Form { public Form1() { InitializeComponent(); MessageBox.Show(new class2().CallMe); } } public class class2 { public string CallMe { get { return HelpMe(); } } private string HelpMe([CallerMemberName] string param = "") { return param; } } 回答1: Targeting 4.0 works just fine if you add:

Is the bindingRedirect .config file needed or all assemblies in an application? [duplicate]

青春壹個敷衍的年華 提交于 2019-12-19 10:26:22
问题 This question already has answers here : Why NuGet adds app.config with assemblyBinding to LIBRARY projects during a NuGet package update? (3 answers) Closed 4 years ago . Using NuGet to install the Microsoft.Bcl.Async package for a solution that has several dlls and just one exe, which uses all other dlls, NuGet automatically creates a .config file (or adds to an existing one) for every single project the Bcl package is added to. In practice I end up with > 20 config files all containing the

Why is there no SortedList<T> in .NET? [closed]

纵然是瞬间 提交于 2019-12-17 16:25:07
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . Why is there only a SortedList<TKey, TValue> which looks more like a dictionary, but no SortedList<T> that is actually just a list that is always sorted? According to the MSDN documentation on SortedList, it is actually internally implemented as a dynamically-sized array of

Why does List<T> implement IReadOnlyList<T> in .NET 4.5?

末鹿安然 提交于 2019-12-17 07:41:30
问题 Why does List<T> implement IReadOnlyList<T> in .NET 4.5? List<T> isn't read only... 回答1: Because List<T> implements all of the necessary methods/properties/etc. (and then some) of IReadOnlyList<T> . An interface is a contract that says "I can do at least these things." The documentation for IReadOnlyList<T> says it represents a read-only collection of elements. That's right. There are no mutator methods in that interface. That's what read-only means, right? IReadOnlyList<T> is used in the