是什么在C#双引号和单引号的区别 [英] What's the difference between double quotes and single quote in C#

查看:128
本文介绍了是什么在C#双引号和单引号的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是用C#双引号和单引号的区别?

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 System.IO;
namespace Consoleapp05
{
    class Program
    {
    	public static void Main(string[] args)
    	{
    		StreamReader sr = new StreamReader(@"C:\words.txt");
    		string text = sr.ReadToEnd();
    		int howmany = 0;
    		int howmany2 = 0;

    		for(int i = 0; i < text.Length; i++)
    		{
    			if(text[i] == " ")
    			{
    			howmany++;
    			}
    		}
    		howmany2 = howmany + 1;
    		Console.WriteLine("It is {0} words in the file", howmany2);
    		Console.ReadKey(true);
    	}
    }
}

这给了我一个错误,因为的双引号。我的老师告诉我使用单引号代替,但他没有告诉我为什么。那么什么是用C#双引号和单引号的区别?

This gives me an error because of the double quotes. My teacher told me to use single quote instead, but he didn't tell me why. So what's the difference between double quotes and single quote in C#?

推荐答案

单引号编码一个字符(数据类型字符),而双引号编码的多个字符的字符串。 。所不同的是类似于单个整数和整数数组之间的区别

Single quotes encode a single character (data type char), while double quotes encode a string of multiple characters. The difference is similar to the difference between a single integer and an array of integers.

char c = 'c';
string s = "s"; // String containing a single character.
System.Diagnostics.Debug.Assert(s.Length == 1);
char d = s[0];

int i = 42;
int[] a = new int[] { 42 }; // Array containing a single int.
System.Diagnostics.Debug.Assert(a.Length == 1);
int j = a[0];

这篇关于是什么在C#双引号和单引号的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆