2019_12_15
#C# Learning notes
##第三章----变量和表达式
#####ToInt32(ReadLine())用处
using static System.Console;
using static System.Convert;
class Progam
{
static void Main()
{
int numA, numB;
WriteLine("Give me 2 numbers :");
numA = ToInt32(ReadLine());
numB = ToInt32(ReadLine());
WriteLine($"The product of {numA} and {numB} is " + $"{numA*numB}.");
ReadKey();
}
}
#####switch用法
using System;
using static System.Console;
class Program
{
static void Main()
{
const string myName = "benjamin";
const string niceName = "andrea";
const string sillyName = "ploppy";
string name;
System.Console.WriteLine("What is your name?");
name = System.Console.ReadLine();
switch (name.ToLower())
{
case myName:
System.Console.WriteLine("You have the same name as me!");
break;
case niceName:
System.Console.WriteLine("My, what a nice name you have!");
break;
case sillyName:
System.Console.WriteLine("That is a very silly name.");
break;
}
WriteLine($"Hello {name}!");
ReadKey();
}
}
question
using static System.Console;
using static System.Convert;
static void Main()
{
bool numbersOK = false;
double var1, var2;
var1 = 0;
var2 = 0;
while(!numbersOK)
{
WriteLine("Enter 2 numbers, both numbers cannot be greater than 10.");
WriteLine("Please enter the first number:);
var1 = ToDouble(ReadLine());
WriteLine("Please enter the second number:");
var2 = ToDouble(ReadLine());
WriteLine($"The first number entered is {var1} " + $"and the second is {var2}");
if((var1 > 10) ^ (var2 > 10))
{
numbersOK = true;
}
else if((var2 > 10) ^ (var1 > 10))
{
numbersOK = true;
}
else
{
WriteLine("Only one number may be greater than 10," + "please try again.");
}
}
ReadKey();
}
##第五章----变量的更多内容
###5.1类型转换
####5.1.1隐式转换
- 规则:任何类型A,只要其取值范围完全包含在类型B的取值范围内,就可以隐式转换为类型B。
eg. ushort和char的值可以互换,因为它们都能存储 0~65535 之间的数字
ushort destinationVar;
char sourceVar = 'a';
destinationVar = sourceVar;
WriteLine($"sourceVar val: {sourceVar}");
WriteLine($"destinationVar val: {destinationVar}");
Output:
sourceVar val: a
destinationVar val: 97
####5.1.2显示转换
#####1.语法
- (<destinationType>)<sourceVar>
#####2.checked 和 unchecked
- 作用:使表达式进行/不进行溢出检查
- 用法:
- checked(expression)
- unchecked(expression)
eg.
byte destinationVar; //byte 存储范围 0~255
short sourceVar = 281; //short 存储范围 0~32767
destinationVar = checked((byte)sourceVar);
WriteLine($"sourceVar val: {sourceVar}");
WriteLine($"destinationVar val: {destinationVar}");
Output:程序崩溃
将第3行改为 destinationVar = unchecked((byte)sourceVar);
Output:
sourceVar val: 281
destinationVar val: 25
285 = 100011001
25 = 000011001
255 = 011111111
- 注意:两个short相乘结果返回int值,因为这个操作的结果很可能大于32767
###5.2复杂的变量类型
####5.2.1 枚举
using static System.Console;
using static System.Convert;
namespace ConsoleApp1
{
enum orientation : byte
{
north = 1,
south = 2,
east = 3,
west = 4
}
class Program
{
static void Main()
{
byte directionByte;
string directionString;
orientation myDirection = orientation.north;
WriteLine($"myDirection = {myDirection}");
directionByte = (byte)myDirection;
directionString = System.Convert.ToString(myDirection);
//这里使用(string)强制类型转换行不通,因为需要进行的处理
//并不仅是把存储在枚举变量中的数据放在string变量中,而是更复杂
//另外,可以使用变量本身的ToString()命令
//directionString = myDirection.ToString();
WriteLine($"byte equivalent = {directionByte}");
WriteLine($"string equivalent = {directionString}");
//也可以把string转化为枚举类型,用法如下:
//(enumerationType)Enum.Parse(typeof(enumerationType),myString);
//eg.
//string myString = "north";
//orientation myDirection = (orientation)Enum.Parse(typeof(orientation),myString);
ReadKey();
}
}
}
_ Output: _
myDirection = north
byte equivalent = 1
string equivalent = north
####5.2.2 结构体
using static System.Console;
using static System.Convert;
namespace ConsoleApp1
{
enum orientation : byte
{
north = 1,
south = 2,
east = 3,
west = 4
}
struct route
{
public orientation direction;
public double distance;
}
class Program
{
static void Main()
{
route myRoute;
int myDirection = -1;
double myDistance;
WriteLine("1) North\n2) South\n3) East\n4) West");
do
{
WriteLine("Select a direction:");
myDirection = ToInt32(ReadLine());
}
while ((myDirection < 1) || (myDirection > 4));
WriteLine("Input a distance:");
myDistance = ToDouble(ReadLine());
myRoute.direction = (orientation)myDirection;
myRoute.distance = myDistance;
WriteLine($"myRoute specifies a direction of {myRoute.direction} " + $"and a distance of {myRoute.distance}");
ReadKey();
}
}
}
####5.2.3 数组
#####1.声明方式:<baseType>[] <name>
与C语言不同
int[] myIntArray = { 5, 9, 10, 2, 99};
int[] myIntArray = new int[5];
int[] myIntArray = new int[5] { 5, 9, 10, 2, 99};
使用这种方法,数组大小必须与元素个数相匹配,例如,
int[] myIntArray = new int[10] { 5, 9, 10, 2, 99};
会编译失败
#####2.foreach循环
static void Main()
{
string[] friendNames = { "Jack", "Kevin", "John" };
WriteLine($"Here are {friendNames.Length} of my friends:");
foreach (string friendName in friendNames)
{
WriteLine(friendName);
}
ReadKey();
}
- _ 与 for 相比 _
- 优势:不存在访问非法元素的危险,不用考虑数组中有多少元素,并确保在循环中使用每个元素。
- 区别:foreach对数组内容进行只读访问,不能改变元素的值
#####3.使用 switch case 表达式进行模式匹配
static void Main()
{
string[] friendNames = { "Todd Anthony", "Kevin Holton", "Shane Laigle", null, "" };
foreach (var friendName in friendNames)
{
switch (friendName)
{
case string t when t.StartsWith("T"):
WriteLine("This friends name starts with a 'T': " + $"{friendName} and is {t.Length - 1} letters long ");
break;
case string e when e.Length == 0:
WriteLine("There is a string in the array with no value");
break;
case null:
WriteLine("There is a 'null' value in the array");
break;
case var x:
WriteLine("This is the var pattern of type: " + $"{x.GetType().Name}");
break;
default:
break;
}
}
int sum = 0, total = 0, counter = 0, intValue = 0;
int?[] myIntArray = new int?[7] { 5, intValue, 9, 10, null, 2, 99 };
foreach (var integer in myIntArray)
{
switch (integer)
{
case 0:
WriteLine($"Integer number '{ total }' has a default value of 0");
total++;
break;
case int value:
sum += value;
WriteLine($"Integer number '{ total }' has a value of {value}");
total++;
counter++;
break;
case null:
WriteLine($"Integer number '{ total }' is null");
total++;
break;
default:
break;
}
}
WriteLine($"{total} total integers, {counter} integers with a" +
$"value other than 0 or null have a sum value of {sum}");
ReadKey();
}
teLine(KaTeX parse error: Expected 'EOF', got '}' at position 137: …break; }̲ } Writ…"{total} total integers, {counter} integers with a" +
$“value other than 0 or null have a sum value of {sum}”);
ReadKey();
}