[C#] 纯文本查看 复制代码
using System;
class Method
{
static void Main()
{
// int x = 101;
// ValueTransfer(x);
// x = 101, GetHashCode = 101
// Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
// Student stu = new Student(){name = "Tim"};
// x = Tim, GetHashCode = 46104728
// Console.WriteLine("x = {0}, GetHashCode = {1}", stu.name, stu.GetHashCode());
// ValueQuote(stu);
// x = Tom, GetHashCode = 46104728
// Console.WriteLine("x = {0}, GetHashCode = {1}", stu.name, stu.GetHashCode());
// int x = 100;
// x = 100, GetHashCode = 100
// Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
// ValueQuoteRef(ref x);
// x = 1, GetHashCode = 1
// Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
// double x = default(double);
// x = 0, GetHashCode = 0
// Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
// ValueQuoteOut(out x);
// x = 100, GetHashCode = 100
// Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
// Borrow stu = new Borrow();
// stu.ShowBook();
// Book book = new Book();
// book.title = "C#";
// 查询书籍名
// if (stu.Query(ref book))
// {
// book.borrow = "A, B, C, GIN";
// Console.WriteLine("----------更改分割线----------");
// }
// else
// {
// Console.WriteLine("--------未更改分割线----------");
// }
// stu.ShowBook();
// 不定长参数
// Parameters(1,2,3,4,1,5,5);
double pi = 3.14159666;
// pi就是this修饰的那个参数
double rpi = pi.Round(3);
// 3.14159666 保留 3 位小数后为 3.142
Console.WriteLine("{0} 保留 {1} 位小数后为 {2}", pi, 3, rpi);
}
class Student
{
public string name {get; set;}
}
class Book
{
public string title{get;set;}
public string borrow{get;set;}
}
class Borrow
{
// books 存放着每本书的借用着
Book[] books =
{
new Book(){title = "C#", borrow="A"},
new Book(){title = "C", borrow="A"},
new Book(){title = "Python", borrow="A, B"}
};
// ref 一个引用实例
// Book temp = null;
// public ref Book Query(string title)
// {
// for (int i = 0; i < books.Length; i++)
// {
// if (title = books[i].title)
// {
// return ref title[i];
// }
// }
// return ref temp;
// }
// Book temp = null;
// 存储一个book地址的信息抛出去,使外部也能更改
public bool Query(ref Book book)
{
for (int i = 0; i < books.Length; i++)
{
if (book.title == books[i].title)
{
book = books[i];
return true;
}
}
return false;
}
// 输出books信息
public void ShowBook()
{
for (int i = 0; i < books.Length; i++)
{
Console.WriteLine("{0,-10} by {1}", books[i].title, books[i].borrow);
}
}
}
// 传值
static void ValueTransfer(int x)
{
x = 100;
// x = 100, GetHashCode = 100
Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
}
// 引用一个自生就是引用类型的值
static void ValueQuote(Student stu)
{
// 可以这样理解
// 创建一个Student stu 然后将外面的stu 复制给内部的stu, stu(内) = stu(外)
// 引用的目标地址相同
stu.name = "Tom";
// x = Tom, GetHashCode = 46104728
Console.WriteLine("x = {0}, GetHashCode = {1}", stu.name, stu.GetHashCode());
}
// ref 参数修饰符 引用传值,[(in 参数修饰符)方法内只读 ]
static void ValueQuoteRef(ref int x)
{
x = 1;
// x = 1, GetHashCode = 1
Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
}
// out 参数修饰符,方法内部必须对参数至少修改一次
static void ValueQuoteOut(out double x)
{
try
{
x = Double.Parse(Console.ReadLine());
}
catch
{
x = 6;
}
// x = <input>, GetHashCode = <input>
Console.WriteLine("x = {0}, GetHashCode = {1}", x, x.GetHashCode());
}
static void Parameters(params int[] array)
{
// foreach (var a in array)
// {
// Console.WriteLine(a);
// }
// array[0] = 1
// array[1] = 2
// array[2] = 3
// array[3] = 4
// array[4] = 1
// array[5] = 5
// array[6] = 5
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("array[{0}] = {1}", i, array[i]);
}
}
}
// 类扩展方法
// 扩展方法必须在顶级静态类中定义
public static class DoubleExtension
{
// 必须是静态的。第一个参数必须使用 this 修饰符
public static double Round(this double a, int digital)
{
return Math.Round(a, digital);
}
}