In C++, we usually see and write code like,
Sample sample=new Sample();
if ( sample == NULL )
{
std::cout<<"Memory allocation failed" << std::endl;
}
But in C#, I rarely see this: (at least I've never seen this)
Sample sample = new Sample();
if ( sample == null )
{
Console.WriteLine("Memory allocation failed\n");
}
Means, in C#, we rarely check if a new
failed or not. Why is it so? Does it have something to do with "In C#, new never fails"? Is there such a thing in C# that new
never fails?
If it fails, then why such "check" is so rare in C#? I'm not talking about OutOfMemoryException
, that is after all exception, not "check". I'm talking about the coding style.