value-type

Object Extension Methods on Value Types

Hi, I have an Extension Method: public static string ToDelimenatedString(this object[] array, string delaminator) {...} The Extension is applied to reference types but not value types. I assume this is because object is nullable. How would I write the method above to target value types, is it even possible without writing it out for ...

Performance of Sorting Reference Type vs Value Types

We were trying to sort a collection of FileInfo objects in .NET. We implemented our IComparer to ensure that FileInfo objects were sorted based on our criteria. We then noticed that performance of sorting the FileInfo objects was many times slower than just ints. On a hunch (and remembering how references work in C) we were able to im...

How to determine if type needs to be boxed?

MSDN docs say that only value types need boxing, but this does not apply to string, which is a value type and does not need to be boxed. I initially tried Type.IsValueType, but since that returns true for string, I can't use it to determine whether a type really needs to be boxed. Are there any other methods you are aware of? Is string t...

Is there a difference between declaring and constructing a value type object?

I've been working in .NET for some time now, but occasionally I still get confused by a disparity between the framework and my prior experience in C++. In .NET, all objects are either value types or reference types. Reference types are allocated on the heap while value types are allocated on the stack (in the current CLR implementation,...

Definitions of "primitive", "value type", "struct", "class", "wrap" in Java and C#

I have been trying to understand the use of "primitives" in Java and C# and the difference between them (if any). I have asked a series of questions on SO and some of the answers seem to confuse the issue rather than clarify it. Some answers (and some MS documentation) appear to provide contradictory statements. From SO http://stackove...

Is it possible to change the default value of a primitive data type?

I recently created a generic Matrix<T> class that acts as a wrapper around a List<List<T>> collection. As far as I can tell, this class is working perfectly. I am running into a slight problem though regarding the default values of the T's. I create an instance of Matrix<int>(3, 3), which creates a 3x3 matrix of ints, all defaulted to 0...

Teaching References in C#

In a couple of weeks, I'll be teaching a class of first-year engineers the salient points of references in C# as part of their first-year programming course. Most of them have never programmed before, and had enough trouble learning objects, so teaching references is going to be an uphill battle. I plan to have lots of examples availab...

Layout of .NET value type in memory

I have the following .NET value types: [StructLayout(LayoutKind.Sequential)] public struct Date { public UInt16 V; } [StructLayout(LayoutKind.Sequential)] public struct StringPair { public String A; public String B; public String C; public Date D; public double V; } I have code that is passing a pointer to a v...

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

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 ascendi...

The stack is an implementation detail, or not?

According to http://msdn.microsoft.com/en-us/library/ms229017.aspx, value types "are allocated on the stack or inline with other structures*". Yet in the stack is an implementation detail, Eric Lippert states that that's an implementation detail. To my understanding, an implementation detail is "a behavior produced by code which may be...

Is copying performed when capturing a value-type into a lambda?

struct SomeStruct { public int Num { get; set; } } class Program { static Action action; static void Foo() { SomeStruct someStruct = new SomeStruct { Num = 5 }; action = () => Console.WriteLine(someStruct.Num); } static void Main() { Foo(); action.Invoke(); } } Is a co...

Returning a value type from a property

Hey, I'm getting confused with what happens on the stack and heap in respect to value type properties in classes. My understanding so far: When you create a class with a structure (value type) like this: class Foo { private Bar _BarStruct; public Bar BarStruct { get {return _BarStruct; } set {_BarStruct = value; } } ...

In C# are the terms "Primitive" and "Literal" interchangeable?

A discussion earlier today led me to question whether or not my understanding of primtives and literals is correct. My understanding is that a literal type is specifically a type which can have a value assigned using a notation that both human and compiler can understand without specific type declarations: var firstName = "John"; // ...

Chaining properties in C# & unexpected results

I was just having a quick read through this article (specifically the bit about why he chose to use structs / fields instead of classes / properties) and saw this line: The result of a property is not a true l-value so we cannot do something like Vertex.Normal.dx = 0. The chaining of properties gives very unexpected results. What...

Using comparison operators, such as '!=' and '==', with generics constrained as value in C#

I have the following code: class Foo<T> where T : struct { private T t; [...] public bool Equals(T t) { return this.t == t; } } When I try to compile, it gives me the following error: Operator '==' cannot be applied to operands of type 'T' and 'T' Why can't it be done? If the constraint was where T : class it would h...

Composite key in Dictionary; override GetHashCode(), Equals etc or use structs?

I have quite a few dictionaries where the key is a composite of several different values (mostly strings and integers). Do I implement these keys as classes (and override GetHashCode(), Equals() etc) or do I use struct instead? ReSharper makes it easy to do the overriding, but the code looks horrible. Are there any performance implicat...

Comparing objects of value type N

What is the best way to compare objects of value type N? So I want to do a String, Integer, DateTime, etc comparison depending on the type of the object. ...

Why is a fixed size buffers (arrays) must be unsafe?

Let's say I want to have a value type of 7 bytes (or 3 or 777). I can define it like that: public struct Buffer71 { public byte b0; public byte b1; public byte b2; public byte b3; public byte b4; public byte b5; public byte b6; } A simpler way to define it is using a fixed buffer public struct Buffer72 { ...

NHibernate and objects with value-semantics

Problem: If I pass a class with value semantics (Equals method overridden) to NHibernate, NHibernate tries to save it to db even though it just saved an entity equal by value (but not by reference) to the database. What am I doing wrong? Here is a simplified example model for my problem: Let's say I have a Person entity and a City enti...

Test if an object is an Enum

I would like to know if 'theObject' is an enum (of any enum type) foreach (var item in Enum.GetValues(theObject.GetType())) { //do something } ...