Home > Net >  Declare a reference type empty variable, we use=null or=default? why
Declare a reference type empty variable, we use=null or=default? why

Time:10-09

 
UserInfoES userInfo_1=default;
UserInfoES userInfo_2=null;

Declare a reference type empty variable, we use=null or=default? What's the difference?

CodePudding user response:


Are generally not null, conveniently used accustomed to

CodePudding user response:

The compiler can tell you, could you, never ignore the error, is on the development environment, so need not worry too much,

CodePudding user response:

Used to null, the default now

CodePudding user response:

General not statement,
Variable declaration had better use nearby principle,

CodePudding user response:

They had no problems, but you declare variables are generally to be used, then in the new place to use

CodePudding user response:

Use null

CodePudding user response:

See good personal habits,
I tend to be null, after all didn't default

CodePudding user response:

I generally don't write, I will empty,
I intentionally empty, because the compiler will have a hint, at least can remind me at least check over all branches and ensure that there is nothing wrong with branching logic are, to catch up on,

You to write in c #, must be a subsequent initialization, also someone with, so he is null or a default actually doesn't matter, just let the compiler, but our own logic to ensure that the branch must conform to the reservation, so simple I don't want to fool the compiler, I prefer to write code

CodePudding user response:

refer to the eighth floor wanghui0380 response:
I generally don't write, I would be empty,
I intentionally empty, because the compiler will have a hint, at least can remind me at least check over all branches and ensure that there is nothing wrong with branching logic are, to catch up on,

You to write in c #, must be a subsequent initialization, also someone with, so he is null or a default actually doesn't matter, just let the compiler, but our own logic to ensure that the branch must conform to the reservation, so I don't want to fool the compiler simple, I prefer writing defensive code


Learned, thank you
  •  Tags:  
  • C#
  • Related