Suggested Videos
Part 3 - Built-in types
Part 4 - String type
Part 5 - Operators
In this video, we will discuss
1. Nullable types in C#
2. Null Coalescing Operator ??
In C# types are divided into 2 broad categories.
Value Types - int, float, double, structs, enums etc
Reference Types – Interface, Class, delegates, arrays etc
By default value types are non nullable. To make them nullable use ?
int i = 0 (i is non nullable, so "i" cannot be set to null, i = null will generate compiler error)
int? j = 0 (j is nullable int, so j=null is legal)
Nullable types bridge the differences between C# types and Database types
Program without using NULL coalescing operator
using System;
class Program
{
static void Main()
{
int AvailableTickets;
int? TicketsOnSale = null;
if (TicketsOnSale == null)
{
AvailableTickets = 0;
}
else
{
AvailableTickets = (int)TicketsOnSale;
}
Console.WriteLine("Available Tickets={0}", AvailableTickets);
}
}
The above program is re-written using NULL coalescing operator
using System;
class Program
{
static void Main()
{
int AvailableTickets;
int? TicketsOnSale = null;
//Using null coalesce operator ??
AvailableTickets = TicketsOnSale ?? 0;
Console.WriteLine("Available Tickets={0}", AvailableTickets);
}
}
Part 3 - Built-in types
Part 4 - String type
Part 5 - Operators
In this video, we will discuss
1. Nullable types in C#
2. Null Coalescing Operator ??
In C# types are divided into 2 broad categories.
Value Types - int, float, double, structs, enums etc
Reference Types – Interface, Class, delegates, arrays etc
By default value types are non nullable. To make them nullable use ?
int i = 0 (i is non nullable, so "i" cannot be set to null, i = null will generate compiler error)
int? j = 0 (j is nullable int, so j=null is legal)
Nullable types bridge the differences between C# types and Database types
Program without using NULL coalescing operator
using System;
class Program
{
static void Main()
{
int AvailableTickets;
int? TicketsOnSale = null;
if (TicketsOnSale == null)
{
AvailableTickets = 0;
}
else
{
AvailableTickets = (int)TicketsOnSale;
}
Console.WriteLine("Available Tickets={0}", AvailableTickets);
}
}
The above program is re-written using NULL coalescing operator
using System;
class Program
{
static void Main()
{
int AvailableTickets;
int? TicketsOnSale = null;
//Using null coalesce operator ??
AvailableTickets = TicketsOnSale ?? 0;
Console.WriteLine("Available Tickets={0}", AvailableTickets);
}
}
These videos have been very useful for me learning c#.
ReplyDeleteThank you for creating them.
Ya thats new thing which i m learn from this blog.....
ReplyDeleteThanks ...
Thanks a ton you made my programming so easy!!!!!!!!!!!!
ReplyDeletefor me it is one of the best tutorial i have see at all thank you venkat for all excuse my bad english please
ReplyDeletecan i Use Null Coalescing Operator with Enums If yes then how, whats the syntax
ReplyDeleteThanks for this.
ReplyDeleteIn the video at 8:09 we can't use the command !AreYouMajor.Value because the program throws an exception.
ReplyDeleteThat is because the AreYouMajor variable is non a boolean anymore. It's a nullable datatype ... so is actualy an object. In this case also the Value of the AreYouMajor object is null, that means is nothing (not a value), and because of the back to front execution the value "null" is not returned to the object because it has nothing to return. So... this kind of check in else if DOES NOT checks if the AreYouMajor has a value (or the value is null) (or the AreYouMajor variable is a nullable variable). The correct way is the second one... to check it if it is equals to false.
sir can you provide me the ppts in editable form
ReplyDeleteOutstanding Teacher that i have ever seen in my life.
ReplyDeleteThanks