Why doesn't incrementing Nullableint throw an exception?(stackoverflow)

2024-08-30 06:58

本文主要是介绍Why doesn't incrementing Nullableint throw an exception?(stackoverflow),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Question:

Could you please explain, why does Console.WriteLine write empty line (Console.WriteLine(null)give me compilation error) and why there isn't NullReferenceException (even a+=1 shouldn't raise it)?

int? a = null;
a++; // Why there is not NullReferenceException? 
Console.WriteLine(a); // Empty line


You're observing the effects of a lifted operator.

From section 7.3.7 of the C# 5 specification:

Lifted operators permit predefined and user-defined operators that operate on non-nullable value types to also be used with nullable forms of those types. Lifted operators are constructed from predefined and user-defined operators that meet certain requirements, as described in the following:

  • For the unary operators + ++ - -- ! ~ a lifted form of an operator exists if the operand and result types are both non-nullable value types. The lifted form is constructed by adding a single ? modifier to the operand and result types. The lifted operator produces a null value if the operand is null. Otherwise, the lifted operator unwraps the operand, applies the underlying operator, and wraps the result.

So basically, a++ in this case is an expression with a result of null (as an int?) and the variable is left untouched.

When you call

Console.WriteLine(a);

that's being boxed into object, which converts it to a null reference, which is printed as an empty line.

on's answer is correct but I would add some additional notes.

Why does Console.WriteLine(null) give a compilation error?

There are 19 overloads of Console.WriteLine and three of them are applicable to a null: the one that takes a string, the one that takes a char[] and the one that takes an object. C# cannot determine which of these three you mean, so it gives an error. Console.WriteLine((object)null)would be legal because now it is clear.

why does Console.WriteLine(a) write an empty line?

a is a null int?. Overload resolution chooses the object version of the method, so the int? is boxed to a null reference. So this is basically the same as Console.WriteLine((object)null), which writes an empty line.

Why there is not NullReferenceException on the increment?

Where's the null reference that you are worried about? a is a null int? which is not a reference type to begin with! Remember, nullable value types are value types, not reference types, so don't expect them to have reference semantics unless they are boxed to a reference type. There is no boxing in the addition.


这篇关于Why doesn't incrementing Nullableint throw an exception?(stackoverflow)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1120112

相关文章

C++第四十七弹---深入理解异常机制:try, catch, throw全面解析

✨个人主页: 熬夜学编程的小林 💗系列专栏: 【C语言详解】 【数据结构详解】【C++详解】 目录 1.C语言传统的处理错误的方式 2.C++异常概念 3. 异常的使用 3.1 异常的抛出和捕获 3.2 异常的重新抛出 3.3 异常安全 3.4 异常规范 4.自定义异常体系 5.C++标准库的异常体系 1.C语言传统的处理错误的方式 传统的错误处理机制:

myEclipse失去焦点时报错Unhandled event loop exception的解决方案

一句话:百度杀毒惹的祸。。。。果断卸载后问题解决。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArra

这个错误说的是一个不可变数组负值给了一个可变的数组。有可能你前面定义的数组是一个可变数组,但是在你其他方法里面用他的时候,他就是一个不可变数组,因为在可变数组拿到别的地方用的时候,他会默认为不可变的,可能这只是一个类里面你只是简单的声明了他吧,并没有进行对他初始化,或者分配什么内存,所以他只是一个不可变的数组,当你在其他地方用他的时候,他就默认为不可变的数组,他可能因为你的没分配内存,而变回不可变

Exception in plugin Android ButterKnife zelezny

所在页面的布局文件命名id有问题,不能有两个下划线,,如tv__name

深入拆解 Java 虚拟机 】Exception异常笔记

【深入拆解 Java 虚拟机 】Exception异常笔记 try-with-resource语法糖finally try-with-resource语法糖 try后对象的close方法都会被运行。 package com.exception.demo;public class Foo implements AutoCloseable {private final Strin

Exception in thread main java.lang.NoClassDefFoundError: org/apache/juli/l

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/juli/l MyEclipse整合免安装版的Tomcat7,------> 看图吧 最后这个就可以在myeclipse里,使用你的tomcat,而不是用.bat打开!!!!

C++异常处理: try,catch,throw,finally的用法

写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使用过异常,但是你习惯使用异常了吗? 现在很多软件都是n*365*24小时运行,软件的健壮性至关重要.  内容导读本文包括2个大的异常实现概念:C++的标准异常和SEH异常. C++标

[置顶]C++异常处理:try,catch,throw,finally的用法

写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使用过异常,但是你习惯使用异常了吗? 现在很多软件都是n*365*24小时运行,软件的健壮性至关重要.  内容导读本文包括2个大的异常实现概念:C++的标准异常和SEH异常. C++标

Java中Exception知识点

Exception 不论代码块是否处于try或者catch块中,只要执行该代码块时出现异常,系统都会自动生成一个异常对象try块里声明的变量是局部变量try和catch块后的花括号不可省略捕获多种异常时,多种异常类型之间用竖线|隔开捕获多种异常时,异常变量有隐式的final修饰,因此不能对异常变量进行赋值如果在异常处理代码中使用System.exit(1)退出虚拟机,则finally块不会执行除

chapter12-异常(Exception)——(作业)——day15

目录 457-异常课后作业 458-异常课后作业2 457-异常课后作业 package chapter12.exception.homework;/*** @author LuHan* @version 1.0*/public class Homework01 {public static void main(String[] args) {try {if(args.l