Click here to Skip to main content
15,885,278 members
Home / Discussions / C#
   

C#

 
GeneralRe: Update text box from serial data Pin
Iskander1234520-Nov-23 19:29
Iskander1234520-Nov-23 19:29 
GeneralRe: Update text box from serial data Pin
jschell21-Nov-23 5:21
jschell21-Nov-23 5:21 
QuestionSystem.Data.SQLite.SQLiteException: 'SQL logic error no such table: Incident' Pin
Member 1612194217-Nov-23 3:09
Member 1612194217-Nov-23 3:09 
AnswerRe: System.Data.SQLite.SQLiteException: 'SQL logic error no such table: Incident' Pin
Dave Kreskowiak17-Nov-23 3:25
mveDave Kreskowiak17-Nov-23 3:25 
AnswerRe: System.Data.SQLite.SQLiteException: 'SQL logic error no such table: Incident' Pin
trønderen17-Nov-23 3:46
trønderen17-Nov-23 3:46 
GeneralRe: System.Data.SQLite.SQLiteException: 'SQL logic error no such table: Incident' Pin
Member 1612194217-Nov-23 4:02
Member 1612194217-Nov-23 4:02 
GeneralRe: System.Data.SQLite.SQLiteException: 'SQL logic error no such table: Incident' Pin
Dave Kreskowiak17-Nov-23 5:33
mveDave Kreskowiak17-Nov-23 5:33 
GeneralC# 12: New opportunities for auto-foot-wounding Pin
Richard Deeming16-Nov-23 0:24
mveRichard Deeming16-Nov-23 0:24 
Just seen this example of C# 12 on Reddit:
C#
public double GPA => grades switch
{
    [] => 4.0,
    [var grade] => grade,
    [.. var all] => all.Average()
};
Seems innocuous enough, but there is a subtle problem: if the array contains more than one element, the property will allocate a copy of the entire array simply to pass to the Average method.

SharpLab[^]:
C#
public double GPA
{
    get
    {
        double[] array = <grades>P;
        if (array != null)
        {
            int num = array.Length;
            if (num != 0)
            {
                if (num == 1)
                {
                    return array[0];
                }
                double[] subArray = RuntimeHelpers.GetSubArray(array, new Range(new Index(0), new Index(0, true)));
                return Enumerable.Average(subArray);
            }
            return 4.0;
        }
        <PrivateImplementationDetails>.ThrowSwitchExpressionException(array);
        double result = default(double);
        return result;
    }
}
NB: The RuntimeHelpers.GetSubArray method[^] has no check to see if it's returning the entire array; it always creates a copy.

It's simple enough to fix:
C#
public double GPA => grades switch
{
    [] => 4.0,
    [var grade] => grade,
    [..] all => all.Average()
};
SharpLab[^]
C#
public double GPA
{
    get
    {
        double[] array = <grades>P;
        if (array != null)
        {
            int num = array.Length;
            if (num != 0)
            {
                if (num == 1)
                {
                    return array[0];
                }
                return Enumerable.Average(array);
            }
            return 4.0;
        }
        <PrivateImplementationDetails>.ThrowSwitchExpressionException(array);
        double result = default(double);
        return result;
    }
}
But good luck spotting the difference in a code review, or explaining to anyone who hasn't spent time spelunking the BCL source repository why [..] all is better than [.. var all]! D'Oh! | :doh:



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: C# 12: New opportunities for auto-foot-wounding Pin
Richard MacCutchan16-Nov-23 1:11
mveRichard MacCutchan16-Nov-23 1:11 
GeneralRe: C# 12: New opportunities for auto-foot-wounding Pin
jschell16-Nov-23 5:18
jschell16-Nov-23 5:18 
QuestionC#, Winforms, Coding Pin
Hoàng Anh Quốc4-Nov-23 0:00
Hoàng Anh Quốc4-Nov-23 0:00 
AnswerRe: C#, Winforms, Coding Pin
Richard MacCutchan4-Nov-23 0:11
mveRichard MacCutchan4-Nov-23 0:11 
AnswerRe: C#, Winforms, Coding Pin
Andre Oosthuizen10-Nov-23 6:28
mveAndre Oosthuizen10-Nov-23 6:28 
QuestionHow to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Fokwa Divine1-Nov-23 4:30
Fokwa Divine1-Nov-23 4:30 
AnswerRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Richard Deeming1-Nov-23 5:47
mveRichard Deeming1-Nov-23 5:47 
GeneralRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Fokwa Divine1-Nov-23 8:51
Fokwa Divine1-Nov-23 8:51 
AnswerRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
jschell2-Nov-23 4:30
jschell2-Nov-23 4:30 
GeneralRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Fokwa Divine3-Nov-23 6:34
Fokwa Divine3-Nov-23 6:34 
Questionc Pin
Gorla Bhagyasree31-Oct-23 23:19
Gorla Bhagyasree31-Oct-23 23:19 
AnswerRe: c Pin
Richard Deeming31-Oct-23 23:56
mveRichard Deeming31-Oct-23 23:56 
AnswerRe: c Pin
Pete O'Hanlon1-Nov-23 21:26
mvePete O'Hanlon1-Nov-23 21:26 
QuestionC# How to grab html from selection of web browser control Pin
Tridip Bhattacharjee from Unknown26-Oct-23 11:15
Tridip Bhattacharjee from Unknown26-Oct-23 11:15 
SuggestionRe: C# How to grab html from selection of web browser control Pin
Richard Deeming26-Oct-23 21:47
mveRichard Deeming26-Oct-23 21:47 
GeneralRe: C# How to grab html from selection of web browser control Pin
Tridip Bhattacharjee from Unknown27-Oct-23 3:38
Tridip Bhattacharjee from Unknown27-Oct-23 3:38 
GeneralRe: C# How to grab html from selection of web browser control Pin
Richard Deeming27-Oct-23 3:51
mveRichard Deeming27-Oct-23 3:51 

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.