0

Hi there I don't know what is this "?" operator. Can anyone explain this for me?

public virtual decimal? abc {get; set;}

user9405863
  • 1,506
  • 1
  • 11
  • 16
Trương Long
  • 100
  • 1
  • 11

3 Answers3

3

This is a shorthand for Nullable<Decimal>. it can be null, you can check .HasValue to see if it is null or you can get .Value for the value itself if there is one.

Z .
  • 12,657
  • 1
  • 31
  • 56
1

The syntax T? is shorthand for Nullable<T>, where T is a value type. You can use any of the syntax.

2 properties are useful in this case. .HasValue and .Value

if(abc.HasValue) a = abc.Value;
if(abc != null) a = abc.Value;
a = abc != null ? abc.Value : 0;

You can also assign null to them.

decimal? abc = null;
Sunil
  • 3,404
  • 10
  • 23
  • 31
Supriya
  • 290
  • 4
  • 15
0

I give a scenario where this nullable type is useful. for example in your case abc, it can hold values like 4.6, 7.9, 9.45 etc.

If you are thinking of initializing value of abc to undefined or null , that is not possible if you define abc as normal decimal like this

public decimal abc { get; set;}

so if you define abc as nullable type like

public decimal? abc { get; set;}

you can assign null to abc like below

abc= null;

or decimal value

abc = 4.567

if you want check whether abc has non- null value there is property to check that

abc.HasValue() this will return true or false

and to get the actual decimal value out of abc you can try like this

if(abc.HasValue())    
var enteredValue = abc.Value

I hope this helps

user9405863
  • 1,506
  • 1
  • 11
  • 16