reference-type

In C# if an object in a list is added to another list, does changing the object in the second list change the same object in the first list?

隐身守侯 提交于 2020-01-11 11:22:07
问题 Say I have a list of Person objects ( List<Person> ) called persons , like this: class Person { public int PersonId { get; set; } // Unique ID for the person loaded from database public string Name { get; set; } } // In a different class public List<Person> Persons = new List<Person>(); // Person objects are subsequently added to the list and then I select some of the Person objects from the list, sort them by a property (e.g. by each person's unique ID PersonId ), and add these selected

Mutable wrapper of value types to pass into iterators

社会主义新天地 提交于 2020-01-02 04:40:27
问题 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 iterator. In other words, whatever calls Foo() above wants to know the end value of valueThatMeansSomething and Foo() may use it itself. Really, I want an integer that is

Why are Value Types created on the Stack and Reference Types created on the Heap?

柔情痞子 提交于 2020-01-01 12:09:43
问题 Programming language books usually explain that value types are created on the stack, and reference types are created on the heap. My question is Why. 回答1: My question is Why. Why do they "explain" that? Because sometimes the authors don't know any better themselves, and sometimes they're too lazy to explain it properly. The truth it rather more complicated. Fortunately, Eric Lippert has written extensively around this: The stack is an implementation detail, part 1 The stack is an

Why are Value Types created on the Stack and Reference Types created on the Heap?

感情迁移 提交于 2020-01-01 12:09:09
问题 Programming language books usually explain that value types are created on the stack, and reference types are created on the heap. My question is Why. 回答1: My question is Why. Why do they "explain" that? Because sometimes the authors don't know any better themselves, and sometimes they're too lazy to explain it properly. The truth it rather more complicated. Fortunately, Eric Lippert has written extensively around this: The stack is an implementation detail, part 1 The stack is an

Why does Nullable<T> not match as a reference type for generic constraints [duplicate]

放肆的年华 提交于 2020-01-01 07:36:05
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Nullable type as a generic parameter possible? I came across a very weird thing with generic type constraints. I have a class like this: public SomeClass<T> where T:class { } However, I've found I can't use nullable types as I'd expect: new SomeClass<int?>(); I get an error that int? must be a reference type. Is Nullable really just a struct with syntactic sugar to make it look like a reference type? 回答1:

In C# are the terms “Primitive” and “Literal” interchangeable?

时光毁灭记忆、已成空白 提交于 2020-01-01 02:15:13
问题 A discussion earlier today led me to question whether or not my understanding of primtives and literals is correct. My understanding is that a literal type is specifically a type which can have a value assigned using a notation that both human and compiler can understand without specific type declarations: var firstName = "John"; // "John" is literal var firstName = (string)"John"; // *if* the compiler didn't understand that "John" // was a literal representation of a string then I // would

Reference cycles with value types?

给你一囗甜甜゛ 提交于 2019-12-28 04:08:10
问题 Reference cycles in Swift occur when properties of reference types have strong ownership of each other (or with closures). Is there, however, a possibility of having reference cycles with value types only ? I tried this in playground without succes ( Error: Recursive value type 'A' is not allowed ). struct A { var otherA: A? = nil init() { otherA = A() } } 回答1: A reference cycle (or retain cycle ) is so named because it indicates a cycle in the object graph: Each arrow indicates one object

Reference Type comparison in C#

时光总嘲笑我的痴心妄想 提交于 2019-12-25 05:24:20
问题 I am trying to understand below problem. I want to know why B == A and C == B are false in the following program. using System; namespace Mk { public class Class1 { public int i = 10; } class Program { static void Main(string[] args) { Class1 A = new Class1(); Class1 B = new Class1(); Class1 C = A; Console.WriteLine(B == A); Console.WriteLine(C == B); } } } Output: False False 回答1: In .NET, classes are reference types . Reference types has two thing. Object and a reference to object . In your

Instantiation and Initialization of Value Types vs. Reference Types

匆匆过客 提交于 2019-12-23 02:07:26
问题 int number = new int(); Questions: For reference types, the new operator creates an instance of the type by allocating memory on the heap then initializes it by calling the type's constructor. As seen above, you could do the same for a value type. To me, the line above means that the constructor int() is called to initialize number with a value. I have read that int is a keyword pointing to the struct System.Int32 . Therefore, in Visual Studio, I navigate to the struct Int32. Lo and behold,

Does C++ treat Class Objects like value types if initialized without the new operator?

牧云@^-^@ 提交于 2019-12-23 01:58:14
问题 Sample code: MyItemType a; MyItemType b; a.someNumber = 5; b = a; cout << a.someNumber << endl; cout << b.someNumber << endl; b.someNumber = 10; cout << a.someNumber << endl; cout << b.someNumber << endl; The output: 5 5 5 10 If a and b were reference types, the last 2 lines would have been 10 and 10 instead of 5 and 10 I guess. Does this mean when you do a declaration like this: AClassType anInstance; it is treated like a value type? ------Here is MyItemType.h------------ #ifndef MYITEMTYPE