"Safe-to-escape examples"

// In this simplest example, we can return 'x' from a method and copy its value to an external field 
// because its safe-to-escape scope is "calling method". In other words, the variable 'x' is allowed
// to 'escape' the current method:
static int CallingMethodExample(SomeClassType c) {
	var x = 123;
	c.AnIntegerProperty = x; // 'x' has safe-to-escape scope of "calling method", so this is permitted
	return x; // 'x' has safe-to-escape scope of "calling method", so this is permitted
}

// In this example, we show what happens when a value/variable has a safe-to-escape scope of "current method"
// (meaning once the current method terminates its lifetime will also terminate). In other words, in this case
// the variable 'x' represents a value that will cease to have meaning once the current method completes:
static Span<int> CurrentMethodExample(SomeRefStructType s) {
	Span<int> x = stackalloc[] { 1, 2, 3 };
	s.AnIntegerSpanProperty = x; // 'x' has a safe-to-escape scope of "current method", which means it is NOT allowed to escape OUTSIDE the current method, so this does not compile
	return x;  // 'x' has a safe-to-escape scope of "current method", which means it is NOT allowed to escape OUTSIDE the current method, so this does not compile
}


Code snippet taken from "Complete C# Quick Reference - C# 11".