value-types

Operations on arbitrary value types

This article describes a way, in C#, to allow the addition of arbitrary value types which have a + operator defined for them. In essence it allows the following code: public T Add(T val1, T val2) { return val1 + val2; } This code does not compile as there is no guarantee that the T type has a definition for the '+' operator, but th...

Are value types immutable by definition?

I frequently read that structs should be immutable - aren't they by definition? Do you consider int to be immutable? int i = 0; i = i + 123; Seems okay - we get a new int and assign it back to i. What about this? i++; Okay, we can think of it as a shortcut. i = i + 1; What about the struct Point? Point p = new Point(1, 2); p.O...

How to distinguish that a type is ValueType Or RefereceType?

Hi some simple types like int, string , ....are easy to realize that they are ValueTypes Or RefrenceTypes. But I wanna to know is there any way to distinguish? ...

Using NHibernate ICompositeUserType with a value type

I have a domain model object which has properties of type System.DateTimeOffset. I'm using a database which doesn't support this type natively, so I'm planning to store it using a column of type 'datetime' and one of type 'smallint'. I've dug around on how to map this using NHibernate components, and found that it could work using an I...

Convert an array of different value types to a byte array

This is what I've come up with so far, but it doesn't seem very optimal, any ideas on better approaches? public void ToBytes(object[] data, byte[] buffer) { byte[] obytes; int offset = 0; foreach (object obj in data) { if (obj is string) obytes = System.Text.Encoding.UTF8.GetBytes(((string)obj)); ...

C#, Copy one bool to another (by ref, not val)

Hi folks. I am at a brick wall here. Is it possible to copy one bool to the ref of another. Consider this code . . . bool a = false; bool b = a; b is now a totally seperate bool with a value of false. If I subsequently change a, it will have no effect on a. Is it possible to make a = b by ref ?How would I do that ? M...

How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes?

C# doesn't allow structs to derive from classes, but all ValueTypes derive from Object. Where is this distinction made? How does the CLR handle this? ...

Is it possible to clone a ValueType?

Is it possible to clone an object, when it's known to be a boxed ValueType, without writing type specific clone code? Some code for reference List<ValueType> values = new List<ValueType> {3, DateTime.Now, 23.4M}; DuplicateLastItem(values); The partical issue I have is with a value stack based virtual instruction machine. (And Im too ...

How to change endianness when unmarshalling a CDR stream containing valuetype objects in Java.

I've got marshaled CDR data all by itself in the form of a file (i.e., not packed in a GIOP message) which I need to unmarshal and display on the screen. I get to know what type the data is and have working code to do this successfully by the following: ValueFactory myFactory = (ValueFactory)myConstructor.newInstance( objParam ); Stream...

Variable number of arguments without boxing the value-types?

public void DoSomething(params object[] args) { // ... } The problem with the above signature is that every value-type that will be passed to that method will be boxed implicitly, and this is serious performance issue for me. Is there a way to declear a method that accepts variable number of arguments without boxing the value-type...

How to create an instance of value types using reflection

I want to create an instance of value types like System.String, System.Boolean, System.Int32, etc. I get qualified names of types like System.String or MyNamespace.Employee and I have to create an instance and return back. I use Activator.CreateInstance and FormatterServices.GetUninitializedObject to create instances. But it fails in cas...

.NET Parameter passing - by reference v/s by value

I'm trying to validate my understanding of how C#/.NET/CLR treats value types and reference types. I've read so many contradicting explanations I stil This is what I understand today, please correct me if my assumptions are wrong. Value types such as int etc live on the stack, Reference types live on the managed heap however if a refer...

C# and VS.NET : Cannot assign property of struct returned from method

I got the following error which I translated from german: error BC30068: The Expression is a value and cannot be target of an assignment. Iam trying to do the following: sheet.Cells(row, col).Value = newVal ' this is VB Where I have declared Cells as: public Cell Cells(int x, int y) // this is C# { return new Cell(this, x, y); ...

Is Guid considered a value type or reference type?

Hi folks, Guids are created using the new keyword which makes me think it's a reference type. Is this correct? Guid uid = new Guid(); Are Guids stored on the heap? ...

How to handle value types when embedding IronPython in C#?

There is a well known issue when it comes to using .NET value types in IronPython. This has recently caused me a headache when trying to use Python as an embedded scripting language in C#. The problem can be summed up as follows: Given a C# struct such as: struct Vector { public float x; public float y; } And a C# class such ...

How does the CLR (.NET) internally allocate and pass around custom value types (structs)?

Question: Do all CLR value types, including user-defined structs, live on the evaluation stack exclusively, meaning that they will never need to be reclaimed by the garbage-collector, or are there cases where they are garbage-collected? Background: I have previously asked a question on SO about the impact that a fluent...

Where are value types defined in a reference type stored (heap or stack) ?

Are the value types defined inside a reference type stored on the heap or on the stack? If stored on the heap, then when are value types stored on the stack? If stored on the stack, then what goes inside the heap as everything ends at a value type in the end? ...

.NET Structure Concept Problem

Why does constructor not required in structure ? Why does GC don't remove structures ? ...

Reference and Value types scenario

I've been playing around trying to thoroughly understand Reference and Value types. Just when I thought I had it, I came across this scenario... I created a class that would contain a single object. class Container { public object A {get; set;} } When I create an instance of this Container class (a) I am creating instance of a re...

Weird behaviour with conditional operator in .Net

This has me pretty stumped. Maybe I'm too tired right now. Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height); Rectangle cropArea = inputArea == null ? rectangle : inputArea.Value; if (inputArea == null) cropArea = rectangle; inputArea is a nullable Rectangle, which in my particular case is null....