Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the nullable types in C #

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what are the types of nullable in C#, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

The Nullable class provides complementary support for Nullable structures. The Nullable class supports getting base types of types that can be null, and comparison and equality operations of type pairs that can be null that do not support generic comparison and equality operations for their underlying value types.

In C #? With null and Nullable

/ / A. For example, in the following sentence, it is wrong to directly define int as null, and the error indicates that null cannot be converted to int, because the latter is a value type that cannot be null.

PrivateintgetNum=null

/ / B. If changed to the following way of writing, it can be initially referred to as null, under certain circumstances? Equivalent to the underlying type of Nullable.

Privateint?getNum=null

PrivateNullablegetNumNull=null

How's NullableClass?

Value types that can be assigned with null are supported. Cannot inherit this class.

C#

[System.Runtime.InteropServices.ComVisible (true)]

PublicstaticclassNullable

Inheritance

Object

Nullable

Attributes

ComVisibleAttribute

Remarks

If a type can be assigned or assigned to a null, the type is called null, which means that the type has no value. By default, all reference types (such as String) can be null, but all value types (such as Int32) are not.

In C # and VisualBasic, by using? after the value type? Notation that marks the value type as null. For example, in VisualBasic, C#int? Or Integer? Declares the type of integer value that can be assigned by null.

Boxing and unboxing

When boxing types that can be null, the common language runtime automatically boxes the underlying values of the Nullable object, rather than the Nullable object itself. That is, if the HasValue attribute is true, the contents of the Value attribute are boxed. If the HasValue property is false, null is boxed. If the base value of the type that can be null is unboxed, the common language runtime creates a new Nullable structure that has been initialized to the base value.

The above is all the content of the article "what are the nullable types in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report