// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");
//using 关键字用于在程序中包含 System 命名空间。 一个程序一般有多个 using 语句。
//using===import
using System;
//struct
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
//一个 namespace 里包含了一系列的类。HelloWorldApplication 命名空间包含了类 HelloWorld。
//防重名
//命名空间可以被嵌套
namespace HelloWorldApplication
{
// 内嵌命名空间
namespace Nested
{
public class NestedNameSpaceClass
{
public static void SayHello()
{
Console.WriteLine("In Nested");
}
}
}
//class 声明。类 HelloWorld 包含了程序使用的数据和方法声明。类一般包含多个方法。方法定义了类的行为。在这里,HelloWorld 类只有一个 Main 方法。
class HelloWorld
{
//enum只能定义在函数外面
enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
// Main 方法,是所有 C# 程序的 入口点。Main 方法说明当执行时 类将做什么动作。
static void Main(string[] args)
{
Nested.NestedNameSpaceClass.SayHello();
int a = 100;
Console.WriteLine("Hello World");
//Console.ReadKey();
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.WriteLine("Please input a char");
Console.ReadLine();
//int类型的大小
Console.WriteLine("Size of int: {0}", sizeof(int));
Console.WriteLine("===============");
HelloWorld h = new HelloWorld();
Console.WriteLine("before: {0}",a);
h.getValue(out a);
Console.WriteLine("after: {0}", a);
Console.WriteLine("=======Nullable========");
int? num1 = null;
int? num2 = 45;
double? num3 = new double?();
double? num4 = 3.1415926;
bool? boolval = new bool?();
Console.WriteLine("Nullable: {0}, {1}, {2}, {3}", num1, num2, num3, num4);
Console.WriteLine("Nullable bool: {0}", boolval);
Console.WriteLine("=======Array========");
double[] balance1;
double[] balance2 = new double[10];
balance2[0] = 4500.0;
double[] balance3 = { 234.0, 456.0, 789.0 };
int[] marks = new int[] { 99, 98, 92, 97, 95 };
int[] score = marks;
/* 初始化数组 n 中的元素 */
for (int i = 0; i < 10; i++)
{
balance2[i] = i + 100;
}
/* 输出每个数组元素的值 */
foreach (int j in balance2)
{
int i = j - 100;
Console.WriteLine("Element[{0}] = {1}", i, j);
}
int[][] scores = new int[2][] { new int[] { 1, 2, 3, 4 }, new int[] { 5, 6, 7, 8 } };
double avg;
avg = h.getAverage(marks, 5);
Console.WriteLine("average = {0}", avg);
int sum = h.addElements(5, 5, 6, 7, 7);
Console.WriteLine("add :{0}", sum);
int[] list = { 34, 72, 13, 44, 25, 30, 10 };
Console.Write("before: ");
foreach (int i in list)
{
Console.Write(i + " ");
}
Console.WriteLine();
// 逆转数组
Array.Reverse(list);
Console.Write("reverse: ");
foreach (int i in list)
{
Console.Write(i + " ");
}
Console.WriteLine();
// 排序数组
Array.Sort(list);
Console.Write("sort: ");
foreach (int i in list)
{
Console.Write(i + " ");
}
Console.WriteLine("=======String========");
//字符串,字符串连接
string fname, lname;
fname = "Rowan";
lname = "Atkinson";
string fullname = fname + lname;
Console.WriteLine("Full Name: {0}", fullname);
//通过使用 string 构造函数
char[] letters = { 'H', 'e', 'l', 'l', 'o' };
string greetings = new string(letters);
Console.WriteLine("Greetings: {0}", greetings);
//方法返回字符串
string[] sarray = { "Hello", "From", "Tutorials", "Point" };
string message = String.Join(" ", sarray);
Console.WriteLine("Message: {0}", message);
//用于转化值的格式化方法
DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
string chat = String.Format("Message sent at {0:t} on {0:D}",
waiting);
Console.WriteLine("Message: {0}", chat);
string str1 = "This is test";
string str2 = "This is text";
Console.WriteLine(String.Compare(str1, str2));//-1
string str3 = "This is test";
Console.WriteLine(String.Compare(str1, str3));//0
string str = "This is test";
if (str.Contains("test"))
{
Console.WriteLine("The sequence 'test' was found.");//The sequence 'test' was found.
}
string str4 = "Last night I dreamt of San Pedro";
Console.WriteLine(str4);//Last night I dreamt of San Pedro
string substr = str4.Substring(23);
Console.WriteLine(substr);//San Pedro
string[] starray = new string[]{"Down the way nights are dark",
"And the sun shines daily on the mountain top",
"I took a trip on a sailing ship",
"And when I reached Jamaica",
"I made a stop"};
string str5 = String.Join("\n", starray);
Console.WriteLine(str5);
//Down the way nights are dark
//And the sun shines daily on the mountain top
//I took a trip on a sailing ship
//And when I reached Jamaica
//I made a stop
Console.WriteLine("=======Struct========");
//1. 结构可带有方法、字段、索引、属性、运算符方法和事件
//2. 结构可定义构造函数,但不能定义析构函数。但是,您不能为结构定义无参构造函数。无参构造函数(默认)是自动定义的,且不能被改变。
//3. 结构不能继承其他的结构或类,可实现一个或多个接口
//4. 当您使用 New 操作符创建一个结构对象时,会调用适当的构造函数来创建结构。与类不同,结构可以不使用 New 操作符即可被实例化。
//5. 结构成员不能指定为 abstract、virtual 或 protected
//6. 如果不使用 New 操作符,只有在所有的字段都被初始化之后,字段才被赋值,对象才被使用。
//结构体和类的区别:
//1. 类是引用类型,结构是值类型
//2. 结构不支持继承
//3. 结构不能声明默认的构造函数,结构体的构造函数中,必须为结构体所有字段赋值
//4. 结构体中声明的字段无法赋予初值,类可以
Console.WriteLine("=======Enum========");
//枚举是一组命名整型常量
//枚举包含自己的值,且不能继承或传递继承。
int x = (int)Day.Sun;
int y = (int)Day.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y);
Console.WriteLine("=======Class========");
Rectangle1 Rect = new Rectangle1();
Rect.setWidth(5);
Rect.setHeight(7);
// 打印对象的面积
Console.WriteLine("总面积: {0}", Rect.getArea());
//============运算符重载===============
Box Box1 = new Box(); // 声明 Box1,类型为 Box
Box Box2 = new Box(); // 声明 Box2,类型为 Box
Box Box3 = new Box(); // 声明 Box3,类型为 Box
Box Box4 = new Box();
double volume = 0.0; // 体积
// Box1 详述
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// Box2 详述
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// 使用重载的 ToString() 显示两个盒子
Console.WriteLine("Box1: {0}", Box1.ToString());
Console.WriteLine("Box2: {0}", Box2.ToString());
// Box1 的体积
volume = Box1.getVolume();
Console.WriteLine("Box1 的体积: {0}", volume);
// Box2 的体积
volume = Box2.getVolume();
Console.WriteLine("Box2 的体积: {0}", volume);
// 把两个对象相加
Box3 = Box1 + Box2;
Console.WriteLine("Box3: {0}", Box3.ToString());
// Box3 的体积
volume = Box3.getVolume();
Console.WriteLine("Box3 的体积: {0}", volume);
//comparing the boxes
if (Box1 > Box2)
Console.WriteLine("Box1 大于 Box2");
else
Console.WriteLine("Box1 不大于 Box2");
if (Box1 < Box2)
Console.WriteLine("Box1 小于 Box2");
else
Console.WriteLine("Box1 不小于 Box2");
if (Box1 >= Box2)
Console.WriteLine("Box1 大于等于 Box2");
else
Console.WriteLine("Box1 不大于等于 Box2");
if (Box1 <= Box2)
Console.WriteLine("Box1 小于等于 Box2");
else
Console.WriteLine("Box1 不小于等于 Box2");
if (Box1 != Box2)
Console.WriteLine("Box1 不等于 Box2");
else
Console.WriteLine("Box1 等于 Box2");
Box4 = Box3;
if (Box3 == Box4)
Console.WriteLine("Box3 等于 Box4");
else
Console.WriteLine("Box3 不等于 Box4");
}
public int addElements(params int[] arr)
{
int sum = 0;
foreach(int j in arr)
{
sum += j;
}
return sum;
}
public double getAverage(int[] arr, int size)
{
int i;
double avg;
int sum = 0;
for (i = 0; i < size; i++)
{
sum += arr[i];
}
avg = (double)sum / size;
return avg;
}
//按值传参,按引用传参,按输出传参
public void getValue(out int x)
{
int temp = 5;
x = temp;
}
//继承exends
//基类
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
public int getArea()
{
Console.WriteLine("father~~~");
return 0;
}
//当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法。
public virtual int area()
{
Console.WriteLine("父类的面积:");
return 0;
}
}
// 基类 PaintCost
// 需覆盖
public interface PaintCost
{
int getCost(int area);
}
// 派生类
class Rectangle1 : Shape, PaintCost
{
public int getArea()
{
//调用父类方法
base.getArea();
return (width * height);
Console.WriteLine("son~~~");
}
public int getCost(int area)
{
return area * 70;
}
//实现虚方法
public override int area()
{
return (width * height);
}
}
//=====================================
//抽象类
abstract class Person
{
abstract public int area();
}
class Tom : Person
{
public override int area()
{
return 0;
}
}
class Rectangle
{
/*
* (1) Pubilc :任何公有成员可以被外部的类访问。
* (2) Private :只有同一个类中的函数可以访问它的私有成员。
* (3) Protected :该类内部和继承类中可以访问。
* (4) internal : 同一个程序集的对象可以访问。
* (5) Protected internal :3 和 4 的并集,符合任意一条都可以访问。
* private < internal/protected < protected internal < public
*/
// 成员变量
//变量
//int
int i =9, j=7, k;
//double
double length;
double width;
//String
String str = "runoob.com";
//C# string 字符串的前面可以加 @(称作"逐字字符串")将转义字符(\)当作普通字符对待
string str1 = @"C:\Windows";
string str2 = "C:\\Windows";
//常量
public const int x = 5;
//成员函数,如果没有指定访问修饰符,则使用类成员的默认访问修饰符,即为 private。
public void Acceptdetails()
{
length = 4.5;
width = 3.5;
//接受来自用户的值
int num;
Console.WriteLine("Please input a number");
num = Convert.ToInt32(Console.ReadLine());
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
Console.WriteLine("i: {0}", i);
Console.WriteLine("Swap");
swap(ref length, ref width) ;
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
}
public void swap(ref double x, ref double y)
{
double temp;
temp = x;
x = y;
y = temp;
}
//类的 析构函数 是类的一个特殊的成员函数,当类的对象超出范围时执行
~Rectangle()
{
Console.WriteLine("对象已删除");
}
}
class Box
{
private double length; // 长度
private double breadth; // 宽度
private double height; // 高度
public double getVolume()
{
return length * breadth * height;
}
public void setLength(double len)
{
length = len;
}
public void setBreadth(double bre)
{
breadth = bre;
}
public void setHeight(double hei)
{
height = hei;
}
// 重载 + 运算符来把两个 Box 对象相加
public static Box operator +(Box b, Box c)
{
Box box = new Box();
box.length = b.length + c.length;
box.breadth = b.breadth + c.breadth;
box.height = b.height + c.height;
return box;
}
public static bool operator ==(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length == rhs.length && lhs.height == rhs.height
&& lhs.breadth == rhs.breadth)
{
status = true;
}
return status;
}
public static bool operator !=(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length != rhs.length || lhs.height != rhs.height
|| lhs.breadth != rhs.breadth)
{
status = true;
}
return status;
}
public static bool operator <(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length < rhs.length && lhs.height
< rhs.height && lhs.breadth < rhs.breadth)
{
status = true;
}
return status;
}
public static bool operator >(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length > rhs.length && lhs.height
> rhs.height && lhs.breadth > rhs.breadth)
{
status = true;
}
return status;
}
public static bool operator <=(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length <= rhs.length && lhs.height
<= rhs.height && lhs.breadth <= rhs.breadth)
{
status = true;
}
return status;
}
public static bool operator >=(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length >= rhs.length && lhs.height
>= rhs.height && lhs.breadth >= rhs.breadth)
{
status = true;
}
return status;
}
public override string ToString()
{
return String.Format("({0}, {1}, {2})", length, breadth, height);
}
}
}
}
C#学习,
最新推荐文章于 2024-09-03 17:22:26 发布