C# class vs struct

Microsoft has the following rules for using struct: Consider defining a structure instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects. Do not define a structure unless the type has all of the following characteristics: It logically represents a single value, similar to primitive types (integer, double, and so on). It has

C#类与结构

Microsoft使用struct有以下规​​则: 考虑定义一个结构而不是类,如果类型的实例很小并且通常是短暂的或者通常嵌入其他对象中的话。 除非类型具有以下所有特征,否则不要定义结构: 它在逻辑上代表一个单值,类似于原始类型(整数,双精度等)。 它的实例大小小于16个字节。 它是不可变的。 它不会经常被装箱。 据我所知,当你想要一个值类型的行为时,你可以创建struct。 当然,这可以让您在分配和传递给函数时复

Why do my array of structs take up so much memory?

Question: How does the Micro Framework allocate memory for an array of structs? BitBucket repository with code to replicate. Context and Detail I'm making a queue using a fixed sized array to insert delays in processing keystrokes from a USB Keyboard. I'm using a struct to represent the key up and down events and the delay. public struct QueuedEvent { public readonly EventType

为什么我的一系列结构占用这么多的内存?

问题: Micro Framework如何为结构数组分配内存? 带有代码复制的BitBucket存储库。 上下文和细节 我正在使用固定大小的阵列进行队列插入处理来自USB键盘的击键延迟。 我使用一个struct来表示关键的向上和向下事件以及延迟。 public struct QueuedEvent { public readonly EventType Type; // Byte public readonly byte KeyPressed; public readonly TinyTimeSpan Delay; // Int16 public re

Why are mutable structs “evil”?

Following the discussions here on SO I already read several times the remark that mutable structs are “evil” (like in the answer to this question). What's the actual problem with mutability and structs in C#? Structs are value types which means they are copied when they are passed around. So if you change a copy you are changing only that copy, not the original and not any other copies

为什么可变结构是“邪恶的”?

在这里讨论之后,我已经多次阅读了可变结构是“邪恶”的评论(就像这个问题的答案一样)。 C#中可变性和结构的实际问题是什么? 结构是值类型,这意味着它们在传递时被复制。 因此,如果您更改副本,则只会更改该副本,而不会更改原始副本,也不会更改任何其他副本。 如果你的结构是不可变的,那么通过值传递产生的所有自动副本将是相同的。 如果你想改变它,你必须有意识地通过用修改的数据创建一个结构体的新实例来完

When are structs the answer?

I'm doing a raytracer hobby project, and originally I was using structs for my Vector and Ray objects, and I thought a raytracer was the perfect situation to use them: you create millions of them, they don't live longer than a single method, they're lightweight. However, by simply changing 'struct' to 'class' on Vector and Ray, I got a very significant performance gai

何时构建答案?

我正在做一个光线追踪器爱好项目,最初我使用的是用于我的Vector和Ray对象的结构体,我认为光线追踪器是使用它们的最佳状态:您创建了数百万个,它们的寿命不会超过一个方法,它们是轻量级的。 但是,通过简单地将Vector和Ray上的'struct'更改为'class',我获得了非常显着的性能提升。 是什么赋予了? 它们都很小(Vector为3个浮点,Ray为2个),不要过度复制。 当然,我会在需要时将它们传递给方法,但这

Why is it okay that this struct is mutable? When are mutable structs acceptable?

Eric Lippert told me I should "try to always make value types immutable", so I figured I should try to always make value types immutable. But, I just found this internal mutable struct, System.Web.Util.SimpleBitVector32 , in the System.Web assembly, which makes me think that there must be a good reason for having a mutable struct. I'm guessing the reason that they did it this way

为什么这个结构是可变的? 什么时候可变结构可以接受?

Eric Lippert告诉我,我应该“尽量使价值类型不可变”,所以我想我应该尽量使价值类型不变。 但是,我在System.Web程序集中发现了这个内部可变结构System.Web.Util.SimpleBitVector32 ,这让我认为有一个可变结构必须有一个很好的理由。 我猜测他们这样做的原因是因为它在测试中表现更好,并且它们保持内在,以防止其被滥用。 但是,这是猜测。 我已经C&P'd这个结构的来源。 这是什么证明设计决定使用可变结构? 一般

Object reference not set to an instance of an object in ASP.NET

This question already has an answer here: What is a NullReferenceException, and how do I fix it? 33 answers Problem : You might be missing ConnectionString SchoolConnectionString1 in your web.config file. Solution : You need to add ConnectionString in your web.config file as below: <connectionStrings> <add name="SchoolConnectionString1" connectionString="Data Source=s

对象引用未设置为ASP.NET中对象的实例

这个问题在这里已经有了答案: 什么是NullReferenceException,以及如何解决它? 33个答案 问题:您的web.config文件中可能缺少ConnectionString SchoolConnectionString1 。 解决方案:您需要在您的web.config文件中添加ConnectionString ,如下所示: <connectionStrings> <add name="SchoolConnectionString1" connectionString="Data Source=serverName;Initial Catalog=DatabaseName;Pers

Check if datagridview cell is null or empty

This question already has an answer here: What is a NullReferenceException, and how do I fix it? 33 answers 我会遍历使用类似下面的东西的单元格.. foreach (DataGridViewRow dgRow in dataGridView1.Rows) { var cell = dgRow.Cells[7]; if (cell.Value != null) //Check for null reference { cell.Style.BackColor = string.IsNullOrEmpty(cell.Value.ToString()) ? Color.Ligh

检查datagridview单元格是否为空或空

这个问题在这里已经有了答案: 什么是NullReferenceException,以及如何解决它? 33个答案 我会遍历使用类似下面的东西的单元格.. foreach (DataGridViewRow dgRow in dataGridView1.Rows) { var cell = dgRow.Cells[7]; if (cell.Value != null) //Check for null reference { cell.Style.BackColor = string.IsNullOrEmpty(cell.Value.ToString()) ? Color.LightCyan :

Add element to null (empty) List<T> Property

This question already has an answer here: What is a NullReferenceException, and how do I fix it? 33 answers Initialize the list in the constructor: class Maps { public Maps() { AllAntsAtMap = new List<Ant>(); } ... } (Since the property is declared in the superclass Maps, I'd do the initialization there rather than in the subclass Quadrangle.) You shoul

将元素添加到null(空)List <T>属性

这个问题在这里已经有了答案: 什么是NullReferenceException,以及如何解决它? 33个答案 在构造函数中初始化列表: class Maps { public Maps() { AllAntsAtMap = new List<Ant>(); } ... } (因为该属性是在超类Maps中声明的,所以我会在那里进行初始化,而不是在子类Quadrangle中。) 您应该在使用之前初始化AllAntsAtMap 。 你可以使用这个构造函数: public Quadrangle() {

C# NullReferenceException was unhandled by user code

This question already has an answer here: What is a NullReferenceException, and how do I fix it? 33 answers Put a breakpoint at: comm.Parameters["@EmployeeID"].Value = employeesList.SelectedItem.Value; When you run you program check the value of: employeesList.SelectedItem Most likely this will be null if no item has been selected. Try checking for a null value before using: i

C#NullReferenceException未被用户代码处理

这个问题在这里已经有了答案: 什么是NullReferenceException,以及如何解决它? 33个答案 在以下位置放置断点: comm.Parameters["@EmployeeID"].Value = employeesList.SelectedItem.Value; 当你运行你的程序检查值: employeesList.SelectedItem 如果未选择任何项目,最有可能为空。 尝试在使用前检查空值: if (employeesList.SelectedItem != null) { comm.Parameters["@EmployeeID"].Value = empl

Get Object Reference Error when trying to use Request.QueryString

This question already has an answer here: What is a NullReferenceException, and how do I fix it? 33 answers 你需要检查空值if (Request.QueryString["id"] != null && Request.QueryString["id"].Equals("1")) { //Do something } You can do this: if(Request.QueryString.Length != 0) { ... } If you try to access elements which are not present, you'll receive an exception. So, since

尝试使用Request.QueryString时获取对象引用错误

这个问题在这里已经有了答案: 什么是NullReferenceException,以及如何解决它? 33个答案 你需要检查空值if (Request.QueryString["id"] != null && Request.QueryString["id"].Equals("1")) { //Do something } 你可以这样做: if(Request.QueryString.Length != 0) { ... } 如果您尝试访问不存在的元素,则会收到异常。 因此,由于QueryString具有Length的属性,因此将其对照0进行检查意味着根本没有查