diff --git a/TupleStudy/Student.cs b/TupleStudy/Student.cs new file mode 100644 index 0000000..9c7ec80 --- /dev/null +++ b/TupleStudy/Student.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace TupleStudy +{ + /// + /// 学生类 + /// + public class Student + { + /// + /// 编号 + /// + public int Id { get; set; } + + /// + /// 姓名 + /// + public string Name{ get; set; } + + /// + /// 年龄 + /// + public UInt16 Age { get; set; } + } +} diff --git a/TupleStudy/TupleStudy.cs b/TupleStudy/TupleStudy.cs new file mode 100644 index 0000000..8427bdf --- /dev/null +++ b/TupleStudy/TupleStudy.cs @@ -0,0 +1,117 @@ +using System; + +namespace TupleStudy +{ + /// + /// Tuple 学习 + /// 元组是一种数据结构,具有特定数量和元素序列。 + /// Tuple是C# 4.0时出的新特性,.Net Framework 4.0以上版本可用。 + /// + public class TupleStudy + { + #region 创建元组 + /* + 注意:仅支持1到7个元组元素,如果有8个元素或者更多,需要使用Tuple的嵌套和Rest属性去实现。 + */ + + /// + /// 利用构造函数创建元组 + /// 灵活,没有个数等限制 + /// + public Tuple GetTuple_Constructor_One() + { + var tuple = new Tuple(1); + + return tuple; + } + + public Tuple GetTuple_Constructor_One(T tupleType) + { + var tuple = new Tuple(tupleType); + + return tuple; + } + + /// + /// 利用静态 Create方法创建元组 + /// 最多支持八个元素 + /// + public Tuple GetTuple_StaticMethod_Tow(T1 tupleValue1, T2 tupleValue2) + { + var tuple = Tuple.Create(tupleValue1,tupleValue2); + + return tuple; + } + + /// + /// 超过8项,使用嵌套 + /// + public Tuple> GetTuple_Constructor_Nest() + { + var nestTuple = new Tuple(8, "嵌套Tuple"); + var tuple = new Tuple>(1, 2, 3, 4, 5, 6, 7, nestTuple); + + + //此处不能使用Tuple.Create()方法 + return tuple; + } + #endregion + + #region 表示一组数据 + + /// + /// 返回一组数据 + /// + public Tuple GetStudentTuple(int id=1,string name="wanggaofeng", uint age=18) + { + var tuple = Tuple.Create(id, name, age); + return tuple; + } + #endregion + + #region 从方法返回多个值 + + /// + /// 方法返回多值 + /// + public Tuple GetEgg() + { + var tuple = Tuple.Create(1, "白色金蛋", 2018001, new decimal(25.50)); + + return tuple; + } + #endregion + + #region 用于单参数方法的多值传递 + /// + /// 砸金蛋 + /// 单参数方法的多值传递 + /// + public bool SmashEgg(object egg) + { + var tuple = egg as Tuple; + if (tuple == null) + { + return false; + } + + if (tuple.Item1 % 2 ==0) + { + return true; + } + else + { + return false; + } + } + #endregion + + #region 缺点 + // 访问元素的时候只能通过ItemX去访问,使用前需要明确元素顺序,属性名字没有实际意义,不方便记忆; + + // 最多有八个元素,要想更多只能通过最后一个元素进行嵌套扩展; + + // Tuple是一个引用类型,不像其它的简单类型一样是值类型,它在堆上分配空间,在CPU密集操作时可能有太多的创建和分配工作。 + #endregion + } +} diff --git a/TupleStudy/TupleStudy.csproj b/TupleStudy/TupleStudy.csproj new file mode 100644 index 0000000..9f5c4f4 --- /dev/null +++ b/TupleStudy/TupleStudy.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + + diff --git a/TupleStudy/ValueTupleStudy.cs b/TupleStudy/ValueTupleStudy.cs new file mode 100644 index 0000000..8c5e5b1 --- /dev/null +++ b/TupleStudy/ValueTupleStudy.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace TupleStudy +{ + /// + /// 值元组 学习 + /// ValueTuple是C# 7.0的新特性之一,.Net Framework 4.7以上版本可用, 其它版本可以用 nuget 添加System.ValueTuple + /// 值元组也是一种数据结构,用于表示特定数量和元素序列,但是是和元组类不一样的,主要区别如下: + /// 值元组是结构,是值类型,不是类,而元组(Tuple)是类,引用类型; + /// 值元组元素是可变的,不是只读的,也就是说可以改变值元组中的元素值; + /// 值元组的数据成员是字段不是属性。 + /// + public class ValueTupleStudy + { + + } +} diff --git a/TupleStudyTest/Properties/AssemblyInfo.cs b/TupleStudyTest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9be1d88 --- /dev/null +++ b/TupleStudyTest/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("TupleStudyTest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TupleStudyTest")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("d8cbdb9a-3eda-4a6b-bb13-e7765b63717e")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/TupleStudyTest/TupleStudyTest.cs b/TupleStudyTest/TupleStudyTest.cs new file mode 100644 index 0000000..cc2e833 --- /dev/null +++ b/TupleStudyTest/TupleStudyTest.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Xunit; +using Xunit.Abstractions; +using Xunit.Extensions; +using Xunit.Sdk; + +using TupleStudy; + +namespace TupleStudyTest +{ + public class TupleStudyTest + { + private TupleStudy.TupleStudy tupleStudy; + + public TupleStudyTest() + { + tupleStudy = new TupleStudy.TupleStudy(); + } + + [Fact] + public void GetTuple_Constructor_One_Test() + { + var tuple = tupleStudy.GetTuple_Constructor_One(); + Assert.Equal(1, tuple.Item1); + } + + [Fact] + public void GetTuple_Constructor_One_Test2() + { + var tuple = tupleStudy.GetTuple_Constructor_One(5); + Assert.Equal(5, tuple.Item1); + + var tuple2 = tupleStudy.GetTuple_Constructor_One("wanggaofeng"); + Assert.Equal("wanggaofeng", tuple2.Item1); + + var student = new Student() { Id = 1, Name = "wanggaofeng", Age = 28 }; + + var studentTuple = tupleStudy.GetTuple_Constructor_One (student); + Assert.Equal(student, studentTuple.Item1); + } + + [Fact] + public void GetTuple_StaticMethod_Tow_Test() + { + var tuple = tupleStudy.GetTuple_StaticMethod_Tow(5,"wanggaofeng"); + Assert.Equal(5, tuple.Item1); + Assert.Equal("wanggaofeng", tuple.Item2); + + var student = new Student() { Id = 1, Name = "wanggaofeng", Age = 28 }; + + var studentTuple = tupleStudy.GetTuple_StaticMethod_Tow("静态方法",student); + Assert.Equal("静态方法", studentTuple.Item1); + Assert.Equal(student, studentTuple.Item2); + } + + [Fact] + public void GetTuple_Constructor_Nest_Test() + { + var tuple = tupleStudy.GetTuple_Constructor_Nest(); + + Assert.Equal(1, tuple.Item1); + Assert.Equal(2, tuple.Item2); + Assert.Equal(3, tuple.Item3); + Assert.Equal(4, tuple.Item4); + Assert.Equal(5, tuple.Item5); + Assert.Equal(6, tuple.Item6); + Assert.Equal(7, tuple.Item7); + + Assert.Equal(8, tuple.Rest.Item1); + Assert.Equal("嵌套Tuple", tuple.Rest.Item2); + } + + [Fact] + public void GetStudentTuple_Test() + { + var tuple = tupleStudy.GetStudentTuple(2,"王高峰",30); + + Assert.Equal(2, tuple.Item1); + Assert.Equal("王高峰", tuple.Item2); + Assert.True(30==tuple.Item3); + } + + [Fact] + public void SashEgg_Test() + { + var tuple1 = new Tuple("错误的参数",1, "白色金蛋", 2018001, new decimal(25.50)); + var reslul1 = tupleStudy.SmashEgg(tuple1); + Assert.False(reslul1); + + var tuple2 = new Tuple(1, "白色金蛋", 2018001, new decimal(25.50)); + var reslul2 = tupleStudy.SmashEgg(tuple2); + Assert.False(reslul2); + + var tuple3 = new Tuple(2, "白色金蛋", 2018001, new decimal(25.50)); + var reslul3 = tupleStudy.SmashEgg(tuple3); + Assert.True(reslul3); + } + } +} diff --git a/TupleStudyTest/TupleStudyTest.csproj b/TupleStudyTest/TupleStudyTest.csproj new file mode 100644 index 0000000..cb40066 --- /dev/null +++ b/TupleStudyTest/TupleStudyTest.csproj @@ -0,0 +1,88 @@ + + + + + + + Debug + AnyCPU + {D8CBDB9A-3EDA-4A6B-BB13-E7765B63717E} + Library + Properties + TupleStudyTest + TupleStudyTest + v4.6.1 + 512 + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + ..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll + + + ..\packages\xunit.assert.2.3.1\lib\netstandard1.1\xunit.assert.dll + + + ..\packages\xunit.extensibility.core.2.3.1\lib\netstandard1.1\xunit.core.dll + + + ..\packages\xunit.extensibility.execution.2.3.1\lib\net452\xunit.execution.desktop.dll + + + + + + + + + + + + + + + + {3bf11d95-1a17-444f-97a8-5536fcc8c015} + TupleStudy + + + + + + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + + + + + \ No newline at end of file diff --git a/TupleStudyTest/ValueTupleStudyTest.cs b/TupleStudyTest/ValueTupleStudyTest.cs new file mode 100644 index 0000000..b635e3c --- /dev/null +++ b/TupleStudyTest/ValueTupleStudyTest.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Xunit; +using Xunit.Abstractions; +using Xunit.Extensions; +using Xunit.Sdk; + +using TupleStudy; + +namespace TupleStudyTest +{ + public class ValueTupleStudyTest + { + private TupleStudy.ValueTupleStudy tupleStudy; + + public ValueTupleStudyTest() + { + tupleStudy = new TupleStudy.ValueTupleStudy(); + } + + [Fact] + public void ValueTuple_Create_Constructor() + { + var emptyTuple = new ValueTuple(); + Assert.IsType(emptyTuple); + + } + + [Fact] + public void Test() + { + ValueTuple aa = new ValueTuple(); + + } + } +} diff --git a/TupleStudyTest/packages.config b/TupleStudyTest/packages.config new file mode 100644 index 0000000..1548b5e --- /dev/null +++ b/TupleStudyTest/packages.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/元组学习.sln b/元组学习.sln new file mode 100644 index 0000000..616c273 --- /dev/null +++ b/元组学习.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27428.2043 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TupleStudy", "TupleStudy\TupleStudy.csproj", "{3BF11D95-1A17-444F-97A8-5536FCC8C015}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TupleStudyTest", "TupleStudyTest\TupleStudyTest.csproj", "{D8CBDB9A-3EDA-4A6B-BB13-E7765B63717E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3BF11D95-1A17-444F-97A8-5536FCC8C015}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3BF11D95-1A17-444F-97A8-5536FCC8C015}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3BF11D95-1A17-444F-97A8-5536FCC8C015}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3BF11D95-1A17-444F-97A8-5536FCC8C015}.Release|Any CPU.Build.0 = Release|Any CPU + {D8CBDB9A-3EDA-4A6B-BB13-E7765B63717E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D8CBDB9A-3EDA-4A6B-BB13-E7765B63717E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D8CBDB9A-3EDA-4A6B-BB13-E7765B63717E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D8CBDB9A-3EDA-4A6B-BB13-E7765B63717E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0F5943F5-6470-4B04-8D36-8138A25CEA55} + EndGlobalSection +EndGlobal