值类型与引用类型
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = 444;
//New一个列表
List<int> ll = new List<int>();
//初始化列表数据,以便测试
for (int i = 6; i >0; i--)
{
ll.Add(i);
}
//把a添加到列表最后
ll.Add(a);
//输出
Console.WriteLine("(改变a的值前)a=" + a);
Console.Write("(改变a的值前)ll列表:");
foreach (int i in ll)
Console.Write(i.ToString() + ",");
Console.WriteLine();
//改变a的值
a = 2;
//输出
Console.WriteLine("(改变a的值后)a=" + a);
Console.Write("(改变a的值后)ll列表:");
foreach (int i in ll)
Console.Write(i.ToString() + ",");
Console.WriteLine();
List<int> kk = new List<int>();
kk = ll;
kk.Add(777);
Console.Write("(给kk列表添加一个值后)ll列表:");
foreach (int i in ll)
Console.Write(i.ToString() + ",");
Console.WriteLine();
Console.Write("(给kk列表添加一个值后)kk列表:");
foreach (int i in kk)
Console.Write(i.ToString() + ",");
Console.WriteLine();
kk.Sort();
Console.Write("(kk列表排序后)ll列表:");
foreach (int i in ll)
Console.Write(i.ToString() + ",");
Console.WriteLine();
Console.Write("(kk列表排序后)kk列表:");
foreach (int i in kk)
Console.Write(i.ToString() + ",");
Console.WriteLine();
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = 444;
//New一个列表
List<int> ll = new List<int>();
//初始化列表数据,以便测试
for (int i = 6; i >0; i--)
{
ll.Add(i);
}
//把a添加到列表最后
ll.Add(a);
//输出
Console.WriteLine("(改变a的值前)a=" + a);
Console.Write("(改变a的值前)ll列表:");
foreach (int i in ll)
Console.Write(i.ToString() + ",");
Console.WriteLine();
//改变a的值
a = 2;
//输出
Console.WriteLine("(改变a的值后)a=" + a);
Console.Write("(改变a的值后)ll列表:");
foreach (int i in ll)
Console.Write(i.ToString() + ",");
Console.WriteLine();
List<int> kk = new List<int>();
kk = ll;
kk.Add(777);
Console.Write("(给kk列表添加一个值后)ll列表:");
foreach (int i in ll)
Console.Write(i.ToString() + ",");
Console.WriteLine();
Console.Write("(给kk列表添加一个值后)kk列表:");
foreach (int i in kk)
Console.Write(i.ToString() + ",");
Console.WriteLine();
kk.Sort();
Console.Write("(kk列表排序后)ll列表:");
foreach (int i in ll)
Console.Write(i.ToString() + ",");
Console.WriteLine();
Console.Write("(kk列表排序后)kk列表:");
foreach (int i in kk)
Console.Write(i.ToString() + ",");
Console.WriteLine();
Console.ReadKey();
}
}
}