C#命名空间,浮点型,static与sleep
命名空间
namespace 与 using
using
关键字仅仅用于引入namespace
,使用之后在程序中使用引入的namespace
中的类、方法就不需要带入namespace
的前缀,直接使用即可.如果需要直接引用namespace
中的类,需要使用using static
语句,以下两个程序的运行效果完全相同:
using System;
namespace Hello
{
public class Program
{
static void Main()
{
int num0 = 0, num1 = 1;
string title = "My first C# program.\n";
System.Console.Write(title);
System.Console.WriteLine("Hello, world!");
System.Console.WriteLine("Test the slots: num0 = {0}, num1 = {1}.", num0, num1);
System.Console.ReadKey();
}
}
}
using static System.Console;
namespace Hello
{
public class Program
{
static void Main()
{
int num0 = 0, num1 = 1;
string title = "My first C# program.\n";
Write(title);
WriteLine("Hello, world!");
WriteLine("Test the slots: num0 = {0}, num1 = {1}.", num0, num1);
ReadKey();
}
}
}
输出均为:
My first C# program.
Hello, world!
Test the slots: num0 = 0, num1 = 1.
namespace可以存储什么?
A namespace cannot directly contain members such as fields or methods.
A namespace can contain other namespaces, structs, and classes.
命名空间不可直接放置方法(函数),字段(变量),可以直接存放其他的命名空间(嵌套),结构体和类.
浮点型与双精度浮点型
单精度浮点数在赋值的时候需要在常量的后面增加一个f,大小写都可以,以示区别于双精度.
static关键字
静态方法是指一个不需要实例化父类就可以调用的方法,直接从类调用.
静态类是不可以实例化的类.可以理解成一个函数库.
静态类基本上与非静态类相同,但存在一个差异:静态类无法实例化. 换句话说,无法使用new运算符创建类类型的变量. 由于不存在任何实例变量,因此可以使用类名本身访问静态类的成员.
static
用于在声明时修饰,用于修饰该静态类和静态方法.
Sleep()
C#中Sleep()
不在时间相关的命名空间中,而是在线程相关的命名空间System.Threading
的Thread
类中.接受一个int
型参数,单位为毫秒.
using System.Threading;
...
Thread.Sleep(1000);