value-type

Is it safe to use a boxed value type as a locker for the lock statement?

无人久伴 提交于 2021-02-08 04:58:32
问题 The documentation of the lock statement is pretty straightforward: lock (x) { // Your code... } where x is an expression of a reference type . So I should not be allowed to pass a value type as locker for a lock . I noticed though that I can use a value type that implements an interface. In other words I can do this: IDisposable locker = default(DisposableStruct); lock (locker) Console.WriteLine("Thread safe"); struct DisposableStruct : IDisposable { public void Dispose() { } } This was

Why does an empty struct in C# consume memory

吃可爱长大的小学妹 提交于 2021-02-05 20:28:00
问题 I always understood structs (value types) contain exactly the number of bytes as defined in the fields of the structure... however, I did some tests and there seems to be an exception for the empty structs: public class EmptyStructTest { static void Main(string[] args) { FindMemoryLoad<FooStruct>((id) => new FooStruct()); FindMemoryLoad<Bar<FooStruct>>((id) => new Bar<FooStruct>(id)); FindMemoryLoad<Bar<int>>((id) => new Bar<int>(id)); FindMemoryLoad<int>((id) => id); Console.ReadLine(); }

Why does an empty struct in C# consume memory

允我心安 提交于 2021-02-05 20:25:22
问题 I always understood structs (value types) contain exactly the number of bytes as defined in the fields of the structure... however, I did some tests and there seems to be an exception for the empty structs: public class EmptyStructTest { static void Main(string[] args) { FindMemoryLoad<FooStruct>((id) => new FooStruct()); FindMemoryLoad<Bar<FooStruct>>((id) => new Bar<FooStruct>(id)); FindMemoryLoad<Bar<int>>((id) => new Bar<int>(id)); FindMemoryLoad<int>((id) => id); Console.ReadLine(); }

why string, array and dictionary in Swift changed to value type

偶尔善良 提交于 2021-02-05 20:19:35
问题 In Objc string, array and dictionary are all reference types, while in Swift they are all value types. I want to figure out what's the reason behind the scenes, for my understanding, no matter it is a reference type or value type, the objects live in the heap in both Objc and Swift. Was the change for making coding easier? i.e. if it is reference type then the pointer to the object might not be nil, so need to check both pointer and the object not nil for accessing the object. While if it is

How to modify the boxed value without creating a new object in C#?

那年仲夏 提交于 2020-05-24 05:05:21
问题 How to modify the boxed value without creating a new object in C#? E.g. if I have object o = 5; and I want to change the value of the boxed 5 to 6 , how can I do that? The o = 6; will create a new object on the heap and assign the reference to that object to the o . Are there any other ways to change the boxed value? 回答1: You can do the "boxing" yourself, than you can modify it. class Box { public int Value { get;set;} } This prevents the automatic boxing. If you define yourself an conversion

Polymorphism (inheritance) and value types

不问归期 提交于 2020-01-23 01:35:06
问题 I have a bunch of types, PixelMeasure , PointMeasure , CentimeterMeasure and so on, that represent a value with a unit. I would like them to have value semantics: e.g. effectively immutable, don't have to worry about memory allocation, and polymorphism: I can return an object of type Measure and can operate on it without knowning what specific kind it is. I would also like to be able to put multiple different Measure s into a container. It seems these are mutually exclusive in C++. For

What is a better way to check that a given object is a particular value type? [closed]

人盡茶涼 提交于 2020-01-15 05:28:09
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . Below are the 2 commonly used approaches to check before unbox. myObject.GetType() == typeof(MyValueType) IL_0001: callvirt System.Object.GetType IL_0006: ldtoken UserQuery.MyValueType IL_000B: call System.Type.GetTypeFromHandle IL_0010: call System.Type.op_Equality myObject

Why does KeyValuePair not override Equals() and GetHashCode()?

北慕城南 提交于 2020-01-13 08:58:07
问题 I was going to use KeyValuePair in a comparison-intensive code and was perplexed checking how it is implemented in .NET (s. below) Why does it not override Equals and GetHashCode for efficiency (and not implement == ) but instead uses the slow reflection-based default implementation? I know that structs/value types have a default implementation based on reflection for their GetHashCode() and Equals(object) methods, but I suppose it is very inefficient compared to overriding equality if you do

.NET ORMs, immutable value objects, structs, default constructors, and readonly properties

喜欢而已 提交于 2020-01-12 18:47:20
问题 I am just getting started with .NET ORMs, to the point where I haven't even decided between Entity Framework and NHibernate. But in both cases, I'm running into a problem in that they seem to want me to compromise the integrity of my domain model in various ways, especially on finer points of C# object design. This is one of several questions on the subject. I am very used to enforcing immutability on appropriate properties with a pattern that looks like this: public class Foo { private

.NET ORMs, immutable value objects, structs, default constructors, and readonly properties

末鹿安然 提交于 2020-01-12 18:47:13
问题 I am just getting started with .NET ORMs, to the point where I haven't even decided between Entity Framework and NHibernate. But in both cases, I'm running into a problem in that they seem to want me to compromise the integrity of my domain model in various ways, especially on finer points of C# object design. This is one of several questions on the subject. I am very used to enforcing immutability on appropriate properties with a pattern that looks like this: public class Foo { private