value-type

What's the difference between struct and class in .Net?

I'm looking for a clear, concise and accurate answer. Ideally as the actual answer, although links to good explanations welcome. ...

Changing the value of an element in a list of structs

I have a list of structs and I want to change one element. For example : MyList.Add(new MyStruct("john"); MyList.Add(new MyStruct("peter"); Now I want to change one element: MyList[1].Name = "bob" However, whenever I try and do this I get the following error: Cannot modify the return value of System.Collections.Generic.List.this[i...

Can you have a Class in a Struct

Is it possible in C# to have a Struct with a member variable which is a Class type? If so, where does the information get stored, on the Stack, the Heap, or both? ...

Why is there no RAII in .NET?

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

How to modify a boxed value type inside a method

I'm tring to build a library for simplifing late binding calls in C#, and I'm getting trouble tring with reference parameteres. I have the following method to add a parameter used in a method call public IInvoker AddParameter(ref object value) { //List<object> _parameters = new List<object>(); _parameters.Add(val...

How to determine if a string is a number in C#

I am working on a tool where I need to convert string values to their proper object types. E.g. convert a string like 2008-11-20T16:33:21Z to a DateTime value. Numeric values like 42 and 42.42 must be converted to an Int32 value and a Double value respectively. What is the best and most efficient approach to detect if a string is an In...

C# - How can I make sure all my structures are initialized??

I am writing an application in C# which is going to do extensive calculations. Everything is going around basic struct - Value. It is basically double with some additional parameters (accuracy etc.) It has to be a struct, because there will be too many of them created to afford heap allocation. Now, I need to make sure they all are corre...

Garbage collection for ValueType wrappers

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

Does calling a method on a value type result in boxing in .NET?

I was just participating in this question: http://stackoverflow.com/questions/436211/is-everything-in-c-an-object And one poster (in comments of accepted answer) seemed to think that performing a method call on a value type resulted in boxing. He pointed me to this article which doesn't exactly specify the use case we're describing. I...

Take an array of any value type as formal parameter

I would like to be able to declare a function as void foo(<any value type>[] data){} in C# 2.0. If I declare it as void foo(ValueType[] data){} it compiles, but then the elements in data[] are treated as though they're derived from object, e.g. I can't say something like fixed (void* pData = data){} I'd like to avoid taking the...

In C#, why is String a reference type that behaves like a value type?

A String is a reference type even though it has most of the characteristics of a value type such as being immutable and having == overloaded to compare the text rather than making sure they reference the same object. Why isn't string just a value type then? ...

Using a Value Type as Model in Partial View in ASP.NET MVC

Is there a way to use something like this: System.Web.Mvc.ViewUserControl<DateTime>? I get an exception that the type is a value type, not a reference type. What is the proper way to resolve this? Thanks. Edit What I am trying to accomplish is having a control that takes a DateTime to render a calendar. I want to pass in the DateTime ...

List<Rectangle> - behaves like there is a boxing

Hi We all know that generic List<> does not box value types. Why on the following code snippet the rects[1] is not affected by Inflate method? If there is no boxing and I want to afect the rect[1] I need to write three lines of code as it is shown - commented. Can someone please explain this? List<Rectangle> rects = new List<Rectangl...

How to make a value type nullable with .NET XmlSerializer?

Let's suppose I have this object: [Serializable] public class MyClass { public int Age { get; set; } public int MyClassB { get; set; } } [Serializable] public class MyClassB { public int RandomNumber { get; set; } } The XmlSerializer will serialize the object like that: <MyClass> <Age>0</age> <MyClassB> <R...

Nullable value types

If a value type is declared nullable, how should I take precautions for this? I.e. if in the constructor I have: public Point3 ( Point3 source ) { this.X = source.X; this.Y = source.Y; this.Z = source.Z; } would it fail, if source was null? ...

Extension methods defined on value types cannot be used to create delegates - Why not?

Extension methods can be assigned to delegates that match their usage on an object, like this: static class FunnyExtension { public static string Double(this string str) { return str + str; } public static int Double(this int num) { return num + num; } } Func<string> aaMaker = "a".Double; Func<string, string> doubler = FunnyExtensio...

Use cases for boxing a value type in C#?

There are cases when an instance of a value type needs to be treated as an instance of a reference type. For situations like this, a value type instance can be converted into a reference type instance through a process called boxing. When a value type instance is boxed, storage is allocated on the heap and the instan...

Mutable wrapper of value types to pass into iterators

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

C# accessing value-type properties like variables

hey there! I'd like to know whether the following is possible with C# properties. I have a class "Transform" that holds a 4x4 matrix in a private member field. Now I want to create a property like this: Matrix m; public Vector3 Position { get { return new Vector3(m[12], m[13], m[14]); } set { m[12] = value....

C# supports value types and reference types, but are they all objects?

Hi, I know C# has both value and reference types, but how can you do a this: int age = 100; string blah = age.ToString(); If age is a value type, how does it have a ToString method on it? Does it get converted to an object ONLY when required internally then? ...