C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
.NET
ADO.NET
Android
ASP.NET
C#
Databases & DBA
Design Patterns & Practices
iOS
Java
OOP/OOD
SharePoint
Software Testing
Web Development
WPF
View All
3
Reply
nullable types example
Sumit Jolly
11y
1.4k
0
Reply
Delete Row
Delete Column
Insert Link
×
Insert
Cancel
Embed YouTube Video
×
Width (%)
Height (%)
Insert
Cancel
Table Options
×
Rows
Columns
First row as header
Create Table
Insert Image
×
Selected file:
Alignment
Left
Center
Right
Select an image from your device to upload
Upload to Server
Cancel
Submit
Nullable types is essential as value type doesn't hold a null valueDeclaring Nullable Type1. Append a question mark, ?, to the type nameDateTime? startDate;2. Either you can assign a normal value or may assign a null value to it;strDate = null;ORstrDate = DateTime.Now;Working with Nullable Typesbool isNull = strDate == null;Console.WriteLine("isNull: " + isNull);The above example shows that you only need to use the equals operator to check for null. You could also make the equality check as part of an if statement, like this:int products;if (unitsInStock == null){products = 0;}else{products = (int)unitsInStock;}Note: Notice the cast operator in the else clause above. An explicit conversion is required when assigning from nullable to non-nullable types.Fortunately, there's a better way to perform the same task, using the coalesce operator, ??, shown below:int availableUnits = unitsInStock ?? 0;The coalesce operator works like this: if the first value (left hand side) is null, then C# evaluates the second expression (right hand side).
Raghav Mehra
11y
0
Nullable types can store null values to the data Type. For example , int age; In a form if the user skips the Age to enter, then the value of age is stored as Zero In this case if we declare as int? age; instead of int age; then the value of age will be null and not zero, which is meaningful.
Rama
11y
0
Int? someID = null; If(someID.HasVAlue) { }
Sumit Jolly
11y
0
What is Jagged Arrays?
What is MultiDictionary?
Message