Przykładem takiego typu jest np. Klasa String albo struktura Decimal jak i wszystkie typy podstawowe takie jak int, long, bool, double. Nie zmieniamy obiektu , tylko za każdym razem tworzymy nowy
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Decimal varDec1 = 10M;
varDec1 = 20M;
}
}
}
Jak widać poniżej . Każde przypisanie to utworzenie nowej struktury .
Nowa struktura może znajdować się pod tym samym adresem pamięci ale to już nie jest ta sama struktura.
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 20 (0x14)
.maxstack 2
.locals init ([0] valuetype [mscorlib]System.Decimal varDec1)
IL_0000: nop
IL_0001: ldloca.s varDec1
IL_0003: ldc.i4.s 10
IL_0005: call instance void [mscorlib]System.Decimal::.ctor(int32)
IL_000a: ldloca.s varDec1
IL_000c: ldc.i4.s 20
IL_000e: call instance void [mscorlib]System.Decimal::.ctor(int32)
IL_0013: ret
}
albo typ podstawowy String
using System;
namespace ConsoleApp27
{
class Program
{
static void Main(string[] args)
{
String str1 = "jakiś tekst1";
String temp = str1;
str1 = "jakiś tekst2";
Console.WriteLine(Object.ReferenceEquals(str1, temp));
}
}
}