value-type

Using Closures to keep track of a variable: Good idea or dirty trick?

懵懂的女人 提交于 2020-01-11 10:37:09
问题 Ok, i have a need to be able to keep track of value type objects which are properties on another object, which cannot be done without having those properties implement an IObservable interface or similar. Then i thought of closures and the famous example from Jon Skeet and how that prints out 9 (or 10) a bunch of times and not an ascending order of numbers. So i thought why not do this: Class MyClass { ... Func<MyValueType> variable; ... public void DoSomethingThatGetsCalledOften() {

Why is there no RAII in .NET?

若如初见. 提交于 2020-01-09 06:05:47
问题 Being primarily a C++ developer the absence of RAII (Resource Acquisition Is Initialization) in Java and .NET has always bothered me. The fact that the onus of cleaning up is moved from the class writer to its consumer (by means of try finally or .NET's using construct) seems to be markedly inferior. I see why in Java there is no support for RAII since all objects are located on the heap and the garbage collector inherently doesn't support deterministic destruction, but in .NET with the

Unboxing for dynamic type

十年热恋 提交于 2020-01-07 08:52:21
问题 Consider the following code: public class Foo1 { public dynamic dowork() { return 10; } } And in my Main , I call like: int i = new Foo1().dowork(); The return value is 10. My question is why no Unboxing is required here?But in watch I've verified the Return Type of dowork is System.Object . 回答1: It is unboxing - but it's doing it implicitly. There's an implicit conversion from any dynamic expression to any type. The exact conversion performed will depend on the execution-time type of the

Garbage collection for ValueType wrappers

本小妞迷上赌 提交于 2020-01-04 04:40:14
问题 Quoting from the MSDN Link for ValueType Class In cases where it is necessary for a value type to behave like an object, a wrapper that makes the value type look like a reference object is allocated on the heap, and the value type's value is copied into it. The wrapper is marked so the system knows that it contains a value type. This means when I code like "integerVariable.ToString();" a wrapper-object created allows using this method and similarly all the other methods of System.Object. Is

C# using properties with value types with Delegate.CreateDelegate

不问归期 提交于 2020-01-04 01:56:06
问题 Using Jon Skeet's article Making reflection fly and exploring delegates as a guide, I am trying to use the Delegate.CreateDelegate method to duplicate properties as delegates. Here's an example class: public class PropertyGetter { public int Prop1 {get;set;} public string Prop2 {get;set;} public object GetPropValue(string propertyName) { var property = GetType().GetProperty(propertyName).GetGetMethod(); propertyDelegate = (Func<object>)Delegate.CreateDelegate(typeof(Func<object>), this,

How to track individual objects, that are out of order, and then Joining() the consecutive ones?

*爱你&永不变心* 提交于 2020-01-02 12:01:34
问题 I'll start by saying is going to be a little tougher than blindly joining byte[] together. My big picture goal is to optimize an application that currently uploads many 512byte pages to a web server (Azure Page Blob), and reduce that to a single large upload of 4Megs or less. See the links at the bottom of this question for more background as to why. The short answer to why: This optimization will increase speed (fewer IOs) and save money over the long term by using Azure sparse files Now for

Mutable wrapper of value types to pass into iterators

社会主义新天地 提交于 2020-01-02 04:40:27
问题 I'm writing an iterator that needs to pass around a mutable integer. public IEnumerable<T> Foo(ref int valueThatMeansSomething) { // Stuff yield return ...; } This nets me "Error 476 Iterators cannot have ref or out parameters". What I need is this integer value to be modified in the iterator and usable by the caller of the iterator. In other words, whatever calls Foo() above wants to know the end value of valueThatMeansSomething and Foo() may use it itself. Really, I want an integer that is

When to use an array of a value type containing a reference types over an array of a reference type?

人走茶凉 提交于 2020-01-02 04:10:07
问题 Suppose I have the following: public class MyElement { } [Serializable] [StructLayout(LayoutKind.Sequential)] struct ArrayElement { internal MyElement Element; } public class MyClass { internal MyElement ComputeElement(int index) { // This method does a lengthy computation. // The actual return value is not so simple. return new MyElement(); } internal MyElement GetChild(ref MyElement element, int index) { if (element != null) { return element; } var elem = ComputeElement(index); if

Why are Value Types created on the Stack and Reference Types created on the Heap?

柔情痞子 提交于 2020-01-01 12:09:43
问题 Programming language books usually explain that value types are created on the stack, and reference types are created on the heap. My question is Why. 回答1: My question is Why. Why do they "explain" that? Because sometimes the authors don't know any better themselves, and sometimes they're too lazy to explain it properly. The truth it rather more complicated. Fortunately, Eric Lippert has written extensively around this: The stack is an implementation detail, part 1 The stack is an

Why are Value Types created on the Stack and Reference Types created on the Heap?

感情迁移 提交于 2020-01-01 12:09:09
问题 Programming language books usually explain that value types are created on the stack, and reference types are created on the heap. My question is Why. 回答1: My question is Why. Why do they "explain" that? Because sometimes the authors don't know any better themselves, and sometimes they're too lazy to explain it properly. The truth it rather more complicated. Fortunately, Eric Lippert has written extensively around this: The stack is an implementation detail, part 1 The stack is an