using SolrNet.Utils;
using System;
using System.Numerics;
Console.WriteLine("Hello world");
//C#预定义数据类型
namespace Wrox
{
class Program1
{
//C#把数据类型分为两种-值类型/引用类型
//值类型-存储在堆栈中,直接存储其值
//引用类型-存储在托管堆上,是对值得引用
//值类型
//1.int
//sbyte/short/int/long/byte/ushort/uint/ulong
long x = 0x12ab;
uint ui = 1234U;
ulong ul = 1234UL;
//数字分隔符-提高可读性,且不添加任何功能
long li = 0x123_4567_89ab_cedf;//下划线会被编译器忽略
//也可把二进制值分配给整数类型
uint binary1 = 0b1111_1110_1101_1011_1010_1001_1000;
//十六进制
uint hexi = 0xefdcba98;
//八进制-没三位使用一个分隔符
uint binary2 = 0b111_110_101_100_011_010_001_000;
//2.浮点类型
//float用于较小的浮点值,要求精度较低。double数据类型大,精度也高
float f = 12.3F;
//3.decimal类型
//可用于表示精度更高的浮点数,是专用类型进行财务计算
decimal d = 12.3M;
//4.字符类型
//char类型的字面量是用单引号括起来的,如果双引号的化,会被编译器认为是字符串。
//一些转义序列--\' \" \\ \0 \b退格 \n换行 \f回车 }
//引用类型
//object string
//在C#中object类型就是最终的父类型,所有内置类型和自己定义的类型都从它派生。
//可使用object引用来绑定任何特定子类型的对象
//object类型实现了许多一般用途的基本方法包括Tostring()。若重写方法,要类提供一个方法,给出类本身的字符串表示
//string
const string str1 = "Hello";
const string str2 = "World";
string str3 = str1 + str2;
//string 是一个引用类型,被分配在堆上,当把一个字符串变量赋予另一个时,会得到对内存中同一个字符串的两个引用。
static void ForEx1()
{
string s1 = "a string";
string s2 = s1;
Console.WriteLine("s1 is" + s1);
Console.WriteLine("s2 is " + s2);
s1 = "another string";
Console.WriteLine("s1 is now" + s1);
Console.WriteLine("s2 is now" + s2);
}//试下,会得出什么结果
//字符串是不可改变的,修改其中一个字符串,就会创建一个全新的string对象,而另一个字符串不发生任何变化
//注意单引号与双引号-char/string
//字符串还有另一种插值格式
public static void ForEx2()
{
string s1 = "a string";
string s2 = s1;
Console.WriteLine($"s1 is {s1}");
Console.WriteLine($"s2 is {s2}");
s1 = "another string";
Console.WriteLine($"s1 is now {s1}");
Console.WriteLine($"s2 is now {s2}");
}
}//想知道更详细的,可以哔哩哔哩上找资源。
class Program2
{
//程序流控制
//条件语句-switch语句-循环-跳转
//1.条件语句 不得不提IF语句
/* if(condition)
statement(s)
else
statement(s)*/
//在条件语句中如果执行多个语句,就需要用花括号把语句组合程一个块
static void ForEx3()
{
int i=0;
if ( i == 0)
{
Console.WriteLine(123);
}
else
{
Console.WriteLine(456);
}
}
//单独使用if,不加else语句,也可以合并elseif子句
//来看这个
static void ForEx4()
{
Console.WriteLine("Type in a string");
string input;
input = Console.ReadLine();
if( input == "")
{
Console.WriteLine("You typed in an empty string.");
}
else if(input.Length<5){
Console.WriteLine("The string had less than 5 characters.");
}else if (input.Length < 10)
{
Console.WriteLine("The string had at least 5 but less than 10 characters");
}
Console.WriteLine("The string was"+input);
}//if表达式必须等于一个布尔值
//2.switch 语句 等值判断 与case相结合
static void ForEx5()
{
int integerA=0;
switch ( integerA) {
case 1: Console.WriteLine("integerA = 1");
break;
case 2: Console.WriteLine("integerA = 2");
break;
case 3: Console.WriteLine("integerA = 3");
break;
default:
Console.WriteLine("integerA = 10");
break;
}//case的值必须是常量表达式,不允许使用变量
}//case的顺序无关紧要
//循环下次再写。
}
}
C#基础-2
最新推荐文章于 2024-09-10 21:41:16 发布