Click here to Skip to main content
15,891,184 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Done with spinners Pin
Nelek8-Dec-19 20:20
protectorNelek8-Dec-19 20:20 
GeneralRe: Done with spinners Pin
dandy729-Dec-19 5:15
dandy729-Dec-19 5:15 
GeneralRe: Done with spinners Pin
Mark_Wallace8-Dec-19 12:47
Mark_Wallace8-Dec-19 12:47 
GeneralRe: Done with spinners Pin
Daniel Pfeffer8-Dec-19 21:35
professionalDaniel Pfeffer8-Dec-19 21:35 
RantThanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
honey the codewitch8-Dec-19 4:03
mvahoney the codewitch8-Dec-19 4:03 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
OriginalGriff8-Dec-19 4:57
mveOriginalGriff8-Dec-19 4:57 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
honey the codewitch8-Dec-19 4:59
mvahoney the codewitch8-Dec-19 4:59 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
Afzaal Ahmad Zeeshan8-Dec-19 6:39
professionalAfzaal Ahmad Zeeshan8-Dec-19 6:39 
Quote:
Okay so I was kind of surprised this compiled:
if (null == default(T)) _array[_head] = default(T);

Where T is a generic type parameter
Strange, why won't it compile? It just says, "if the default value for this type parameter is null, then set the element at the _head in the _array member as the default value of type T."

To prevent this code from being compiled, you should apply the constraints, that would limit the type that can be used here. C# provides class, struct, notnull and many more as a constraint in this case. To prevent the code from being compiled, you should consider these.
C#
class GenericArray<T> where T : class
{
    public void Insert(T parameter)
    {
        if(default(T) == null)
        {
            Console.WriteLine("null");
        }
    }
}
This code would not accept a value-type (System.ValueType) in the parameter, and would prevent the code from building; note that it is not the line you expect to break the build.

But remember, the application of these constraints would also make your if...else blocks less sensible. For example, if you apply the class constraint then the condition is always true, and the value will always be set to null in the head element, which would be repetition as the default value is set to null for you. Similarly, application of struct as a condition would make this line of yours not build.
C#
class GenericArray<T> where T : struct
{
    public void Insert(T parameter)
    {
        if(default(T) == null) // <--- Error here.
        {
            Console.WriteLine("null");
        }
    }
}

I like reading the template programming concepts, although C# is no way near template programming (such as in C++), the promise of template programming is huge.

Template metaprogramming - Wikipedia

Quote:
It's too bad I don't know if that operation boxes or not.
One of the times when I really miss the old MSDN documentation which was professional, and always accurate. Now, anybody (including peeps like me) can edit the document from one point-of-view and mess it from all other angles. I recommend adding these points to the Microsoft Docs, you can do that with a GitHub account. Thumbs Up | :thumbsup:
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~

GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
honey the codewitch8-Dec-19 6:42
mvahoney the codewitch8-Dec-19 6:42 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
Sander Rossel8-Dec-19 8:17
professionalSander Rossel8-Dec-19 8:17 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
honey the codewitch8-Dec-19 8:18
mvahoney the codewitch8-Dec-19 8:18 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
Sander Rossel8-Dec-19 8:37
professionalSander Rossel8-Dec-19 8:37 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
honey the codewitch8-Dec-19 8:42
mvahoney the codewitch8-Dec-19 8:42 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
Sander Rossel8-Dec-19 9:35
professionalSander Rossel8-Dec-19 9:35 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
honey the codewitch8-Dec-19 9:36
mvahoney the codewitch8-Dec-19 9:36 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
Sander Rossel8-Dec-19 9:45
professionalSander Rossel8-Dec-19 9:45 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
PIEBALDconsult8-Dec-19 8:19
mvePIEBALDconsult8-Dec-19 8:19 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
Dave Kreskowiak8-Dec-19 8:48
mveDave Kreskowiak8-Dec-19 8:48 
GeneralRe: Thanks once again Microsoft, this time for not documenting C#/.NET behavior Pin
honey the codewitch8-Dec-19 8:58
mvahoney the codewitch8-Dec-19 8:58 
GeneralThis is why I don't talk to people... Pin
Sander Rossel8-Dec-19 1:39
professionalSander Rossel8-Dec-19 1:39 
GeneralRe: This is why I don't talk to people... Pin
Cp-Coder8-Dec-19 1:55
Cp-Coder8-Dec-19 1:55 
GeneralRe: This is why I don't talk to people... Pin
Sander Rossel8-Dec-19 2:08
professionalSander Rossel8-Dec-19 2:08 
GeneralRe: This is why I don't talk to people... Pin
OriginalGriff8-Dec-19 2:04
mveOriginalGriff8-Dec-19 2:04 
GeneralRe: This is why I don't talk to people... Pin
Sander Rossel8-Dec-19 2:35
professionalSander Rossel8-Dec-19 2:35 
GeneralRe: This is why I don't talk to people... Pin
Nelek8-Dec-19 3:47
protectorNelek8-Dec-19 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.