0
点赞
收藏
分享

微信扫一扫

C#入门4.7——其他运算符


1.字符串连接运算符+

将两个字符串连在一起组成一个新的字符串

str1="my name is"  str2="jojo";

string str3=str1+str2;

2.is运算符

用于动态检查对象的运行时类型是否与给定类型兼容。

bool=a is string

判断is左边的变量是否与is右边的类型相同。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{

int a=10;
double b=10.0;
string c="hello";
bool mybool=false;
mybool = a is int;
Console.WriteLine("a is int.\t" + mybool);
mybool = b is double;
Console.WriteLine("b is double.\t" + mybool);
mybool = c is string;
Console.WriteLine("c is string.\t" + mybool);
Console.ReadKey();
}
}
}

\t的意思是跳到下一个制表位

3.三元运算符

表达式1?表达式2:表达式3;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine("请输入你拥有钢笔的数量");
int qty = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("I have {0} pen{1}.",qty,qty>1?"s":"");
Console.ReadKey();
}
}
}







举报

相关推荐

0 条评论