Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: Monospaced font (i.e. "Terminal" font in FontDialog Pin
Richard Andrew x6415-Jun-23 14:56
professionalRichard Andrew x6415-Jun-23 14:56 
GeneralRe: Monospaced font (i.e. "Terminal" font in FontDialog Pin
jschell16-Jun-23 9:58
jschell16-Jun-23 9:58 
GeneralRe: Monospaced font (i.e. "Terminal" font in FontDialog Pin
trønderen19-Jun-23 8:48
trønderen19-Jun-23 8:48 
QuestionReferencing an Array from another Class Pin
Member 1602947114-Jun-23 5:44
Member 1602947114-Jun-23 5:44 
AnswerRe: Referencing an Array from another Class Pin
OriginalGriff14-Jun-23 6:00
mveOriginalGriff14-Jun-23 6:00 
AnswerRe: Referencing an Array from another Class Pin
jschell14-Jun-23 6:11
jschell14-Jun-23 6:11 
QuestionHow to use switch case instead of if statements? Pin
Member 1405587912-Jun-23 0:28
Member 1405587912-Jun-23 0:28 
AnswerRe: How to use switch case instead of if statements? Pin
Richard Deeming12-Jun-23 1:06
mveRichard Deeming12-Jun-23 1:06 
That depends on which language version you're using. Relational patterns[^] were added in C# 9.0:
C#
switch (number)
{
    case > 70:
    {
        ...
        break;
    }
    case > 50:
    {
        ...
        break;
    }
    case > 30:
    {
        ...
        break;
    }
    case > 20:
    {
        ...
        break;
    }
}
NB: Unlike older switch statements, the order of the cases matters. The first matching case will be applied, meaning that you need to reverse the order of your tests - 71 is greater than 20, so if case > 20 was first, that would be the case that matched.

Also note that you're using if rather than else if, so the code code > 20 will also execute for values greater than 70. There doesn't seem to be a goto case ... syntax for relational patterns, so if that's the required behaviour, you would need to duplicate the code in each case:
C#
switch (number)
{
    case > 70:
    {
        // Process > 70
        // Process > 50
        // Process > 30
        // Process > 20
        break;
    }
    case > 50:
    {
        // Process > 50
        // Process > 30
        // Process > 20
        break;
    }
    case > 30:
    {
        // Process > 30
        // Process > 20
        break;
    }
    case > 20:
    {
        // Process > 20
        break;
    }
}
This would potentially make the code messier than your current version.

And if the code in the if blocks modifies the number variable in any way, it gets even messier.



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

AnswerRe: How to use switch case instead of if statements? Pin
jschell12-Jun-23 11:09
jschell12-Jun-23 11:09 
QuestionClean code in C# development Pin
WeiminYu10-Jun-23 18:27
WeiminYu10-Jun-23 18:27 
AnswerRe: Clean code in C# development Pin
OriginalGriff10-Jun-23 18:31
mveOriginalGriff10-Jun-23 18:31 
GeneralRe: Clean code in C# development Pin
Richard Andrew x6411-Jun-23 7:17
professionalRichard Andrew x6411-Jun-23 7:17 
GeneralRe: Clean code in C# development Pin
OriginalGriff11-Jun-23 8:32
mveOriginalGriff11-Jun-23 8:32 
GeneralRe: Clean code in C# development Pin
trønderen11-Jun-23 11:20
trønderen11-Jun-23 11:20 
QuestionGetting Google Contacts Pin
Kevin Marois6-Jun-23 16:46
professionalKevin Marois6-Jun-23 16:46 
AnswerRe: Getting Google Contacts Pin
OriginalGriff6-Jun-23 18:47
mveOriginalGriff6-Jun-23 18:47 
GeneralRe: Getting Google Contacts Pin
Kevin Marois7-Jun-23 5:51
professionalKevin Marois7-Jun-23 5:51 
AnswerRe: Getting Google Contacts Pin
Richard MacCutchan6-Jun-23 22:02
mveRichard MacCutchan6-Jun-23 22:02 
GeneralRe: Getting Google Contacts Pin
jschell7-Jun-23 5:19
jschell7-Jun-23 5:19 
GeneralRe: Getting Google Contacts Pin
Richard MacCutchan7-Jun-23 5:42
mveRichard MacCutchan7-Jun-23 5:42 
GeneralRe: Getting Google Contacts Pin
Kevin Marois7-Jun-23 5:53
professionalKevin Marois7-Jun-23 5:53 
GeneralRe: Getting Google Contacts Pin
Richard MacCutchan7-Jun-23 6:09
mveRichard MacCutchan7-Jun-23 6:09 
GeneralRe: Getting Google Contacts Pin
Kevin Marois7-Jun-23 7:00
professionalKevin Marois7-Jun-23 7:00 
GeneralRe: Getting Google Contacts Pin
jschell8-Jun-23 6:53
jschell8-Jun-23 6:53 
GeneralRe: Getting Google Contacts Pin
Richard MacCutchan8-Jun-23 8:01
mveRichard MacCutchan8-Jun-23 8:01 

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.