What's the difference between double quotes and single quote in C#

后端 未结 5 1116
天命终不由人
天命终不由人 2020-12-02 21:49

What\'s the difference between double quotes and single quote in C#?

I coded a program to count how many words are in a file

using System;
using Syst         


        
5条回答
  •  有刺的猬
    2020-12-02 22:39

    when you say string s = "this string" then s[0] is a char at at specific index in that string (in this case s[0] == 't')

    So to answer your question, use double quotes or single quotes, you can think of the following as meaning the same thing:

    string s = " word word";
    
    // check for space as first character using single quotes
    if(s[0] == ' ') {
     // do something
    }
    
    // check for space using string notation
    if(s[0] == " "[0]) {
     // do something
    }
    

    As you can see, using a single quote to determine a single char is a lot easier than trying to convert our string into a char just for testing.

    if(s[0] == " "[0]) { 
     // do something
    }
    

    is really like saying:

    string space = " ";
    if(s[0] == space[0]) {
     // do something
    }
    

    Hopefully I did not confuse you more!

提交回复
热议问题