1、命名空间
可以认为类是属于命名空间的。
如果在当前的项目中没有这个类的命名空间,需要我们手动的导入这个类所在的命名空间。
1)、鼠标点击
2)、shift+alt+F10
2、在一个项目中命名空间的引用关系
1)、引用---> 项目
2)、using _01复习
3)类的保护级别
Program类代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using _01复习1;//下滑线自动添加
namespace _02命名空间
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
_01复习1.Person person = new _01复习1.Person("z",10);
person.SayHellow();
Console.ReadKey();
}
}
}
3、值类型和引用类型
1)值类型和引用类型在内存上存储的地方不一样;
2)在传递值类型和传递引用类型的时候,传递方式不一样,值类型称之为值传递,引用类型称之为引用传递;
值类型:int double char bool decimal struct enum
引用类型:string 自定义类型、数组
1)值类型的值是存储在内存堆栈当中
2)引用类型的值是存储在内存中
3)字符串的不可变性
4)我们可以将字符串看做是char类型的一个只读数组;
既然可以将string看做char类型的制度数组,所以我可以通过下标访问字符串中的某一个元素
TocharArray()将字符串转化为char数组、new string(char[] chs)能够将字符数组转化为字符串。
修改字符串中的某一字符方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03值类型和引用类型
{
class Program
{
static void Main(string[] args)
{
string str = "adfs";
//str[0] = 'b';//错误原因只读类型
//首先将字符串转化为char类型数组
char[] chs = str.ToCharArray();
chs[0] = 'b';
//将字符数组转化为字符串
str=new string(chs);
Console.WriteLine(str);
Console.ReadKey();
}
}
}
4、计算程序运行时间
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04计算程序运行时间
{
class Program
{
static void Main(string[] args)
{
StringBuilder stringBuilder = new StringBuilder();//不开辟空间,运算速度更快
string str = null;
//创建一个定时器
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();//计时器开始
for (int i = 0; i < 100000; i++)
{
//str += i;
stringBuilder.Append(i).ToString();
}
stopwatch.Stop();//计时器结束
Console.WriteLine(stringBuilder.ToString());
Console.WriteLine(stopwatch.Elapsed);
Console.ReadKey();
}
}
}
5、字符串提供的各种方法
1)、Length:获得当前字符串中字符的个数
2)、ToUpper():将字符转换成大写形式
3)、ToLower():将字符串转换成小写形式
4)、Equal是():比较两个字符串的,可以忽略大小写Cur1.Equal(Cur2 , String Comparison. OrdinalIgnoreCase)
5)、Split分割字符串
6)、Replace 字符串的的替换
7)、bool Contains(string value)判断一个字符串当中是否含有子串value
8)、bool StartsWith(string value)判断字符串是否以子串value开始
9)、bool EndsWith(string value)判断字符串是否以子串结束
10)、int IndexOf(string value):取子串value第一次出现的位置
11)、substring截取字符串
12)、LastIndesOf()获取最后一个字符所在的位置
13)、Trim()去除字符串前后的空格
14)、StartTrim()去除前面空格
15)、EndTrim()去除后面空格
16)、string.IsNullOrEmpty判断字符串是否为空
17)、Joint()按照字符串格式连接字符串
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _05字符串练习
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入你喜欢的名字");
string name = Console.ReadLine();
Console.WriteLine("当前名字个数:"+name.Length);
Console.ReadKey();
//Console.WriteLine("请输入喜欢的课程");
//string Cur1 = Console.ReadLine();
//Console.WriteLine("请输入喜欢的课程");
//string Cur2 = Console.ReadLine();
if(Cur1.ToUpper()==Cur2.ToUpper())//字符串转换为大写
{
Console.WriteLine("他们都喜欢的课程为:" + Cur2.ToLower());//转换字符串小写
}
//if (Cur1.Equals(Cur2, StringComparison.OrdinalIgnoreCase))
//{
// Console.WriteLine("课程相同");
//}
//else
//{
// Console.WriteLine("课程不相同");
//}
//Console.ReadKey();
//分割字符串、
//string str = "sd nd_+,v";
//char[] chs = { ' ', '+', '_' };
//string[] str1 = str.Split(chs, StringSplitOptions.RemoveEmptyEntries);
//str = str.Substring(1, 4);//从字符串第一个开始到第四个结束
//Console.WriteLine(str);
//Console.ReadKey();
string[] str = { "张三","李四", "王五" };
string strNewName = string.Join("|", str);
Console.WriteLine(strNewName);
Console.ReadKey();
}
}
}