0
float a = 3f; // presumably the same as float a = new float(3f);
float b = a;

I want 'b' to be a brand new float and not now be a reference to 'a'.

How do I do this?

Stefan
  • 14,530
  • 4
  • 55
  • 62
alan2here
  • 3,223
  • 6
  • 37
  • 62

4 Answers4

6

floats are value types not reference types so b is a new float and changing a won't affect b.

check this one: C# value types

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • Epic, well inconsistant but usefull to know, ty. – alan2here Oct 17 '11 at 11:48
  • 1
    @alan2here: In what way is it inconsistent? Note that even for reference types, changing the value of `a` won't change the value of `b` - although if you change the data within an object that `a` refers to (again, assuming a mutable reference type) then that change will be visible through `b`. – Jon Skeet Oct 17 '11 at 12:07
  • Inconsistant as every complex type, so nearly everything you use is a reference type where-as the few POD types are value types. – alan2here Oct 17 '11 at 21:15
2

Since float type is a Value Type it will be a completely separate variable and does not reference a in any way. Refernce term does not makes sense for Value Types.

See Value Types on MSDN:

The value types consist of two main categories:

  1. Structs
  2. Enumerations

Structs fall into these categories:

  • Numeric types
  • Integral types
  • Floating-point types
  • decimal
  • bool
  • User defined structs.
sll
  • 61,540
  • 22
  • 104
  • 156
2

I am going to demonstrate using your own code, that this already works the way you want it to:

using System;
public sealed class Program{

    public static void Main()
    {
        float a = 3f; // presumably the same as float a = new float(3f);
        float b = a;
        a = a + 1;
        Console.WriteLine("A:{0}", a);//prints 4
        Console.WriteLine("B:{0}", b);//prints 3
    }
}

And yes, as other people have pointed out, this works this way because float is a value type. Value types are stack allocated. This means that each variable that you declare and define as a value type will have its own space on the stack. On the reference types are heap allocated. This means that these variables dont really have the actual values that they represent on the stack. But they are just pointers to the actual objects on the heap. Look at MSDN's System.ValueType class for more clarity.

EDIT 1: I was wrong about how value types are ALWAYS stored on the stack. Anyone reading this should also read this question and this post by Eric Lippert to understand what actually happens under the covers. In this one case, the official Microsoft documentation (link shared above) seems to be wrong or at least capable of misinterpretation.

Community
  • 1
  • 1
gprasant
  • 15,589
  • 9
  • 43
  • 57
1

Just go ahead with your code.

Numeric values are copied when a new variable is assigned to an existing one.

Proof of fact:

Do this in LinqPad

float a = 3f;
float b = a;

b = 3.1f;

a.Dump();
b.Dump();

This is the result:

3
3.1

Community
  • 1
  • 1
Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108
  • LinqPad is a small utility for testing .NET code on the fly. I use it for proof-of-fact situations and also as a test-bed for code. Here's the link to their site http://www.linqpad.net/ . It is also available for direct download here: http://www.linqpad.net/GetFile.aspx?LINQPad4.zip – Alex Essilfie Oct 17 '11 at 11:57
  • @HenkHolterman: It's actually not about your question containing LINQ. What I mean is LinqPad is an application for testing code snippets. It can be used to test *virtually* any .NET code that is why I made reference to it. It saves you the hassle of having to create a new project for any small snippet you'd like to test. – Alex Essilfie Oct 17 '11 at 12:07