From bf6a1aed692fd9634a1b7b58e49f090176bfa011 Mon Sep 17 00:00:00 2001 From: bicijinlian <bicijinlian@163.com> Date: Mon, 16 Jul 2018 19:08:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0VS=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TupleStudy/Student.cs | 27 +++++ TupleStudy/TupleStudy.cs | 117 ++++++++++++++++++++++ TupleStudy/TupleStudy.csproj | 7 ++ TupleStudy/ValueTupleStudy.cs | 19 ++++ TupleStudyTest/Properties/AssemblyInfo.cs | 36 +++++++ TupleStudyTest/TupleStudyTest.cs | 104 +++++++++++++++++++ TupleStudyTest/TupleStudyTest.csproj | 88 ++++++++++++++++ TupleStudyTest/ValueTupleStudyTest.cs | 40 ++++++++ TupleStudyTest/packages.config | 11 ++ 元组学习.sln | 31 ++++++ 10 files changed, 480 insertions(+) create mode 100644 TupleStudy/Student.cs create mode 100644 TupleStudy/TupleStudy.cs create mode 100644 TupleStudy/TupleStudy.csproj create mode 100644 TupleStudy/ValueTupleStudy.cs create mode 100644 TupleStudyTest/Properties/AssemblyInfo.cs create mode 100644 TupleStudyTest/TupleStudyTest.cs create mode 100644 TupleStudyTest/TupleStudyTest.csproj create mode 100644 TupleStudyTest/ValueTupleStudyTest.cs create mode 100644 TupleStudyTest/packages.config create mode 100644 元组学习.sln 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 +{ + /// <summary> + /// 学生类 + /// </summary> + public class Student + { + /// <summary> + /// 编号 + /// </summary> + public int Id { get; set; } + + /// <summary> + /// 姓名 + /// </summary> + public string Name{ get; set; } + + /// <summary> + /// 年龄 + /// </summary> + 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 +{ + /// <summary> + /// Tuple 学习 + /// 元组是一种数据结构,具有特定数量和元素序列。 + /// Tuple是C# 4.0时出的新特性,.Net Framework 4.0以上版本可用。 + /// </summary> + public class TupleStudy + { + #region 创建元组 + /* + 注意:仅支持1到7个元组元素,如果有8个元素或者更多,需要使用Tuple的嵌套和Rest属性去实现。 + */ + + /// <summary> + /// 利用构造函数创建元组 + /// 灵活,没有个数等限制 + /// </summary> + public Tuple<int> GetTuple_Constructor_One() + { + var tuple = new Tuple<int>(1); + + return tuple; + } + + public Tuple<T> GetTuple_Constructor_One<T>(T tupleType) + { + var tuple = new Tuple<T>(tupleType); + + return tuple; + } + + /// <summary> + /// 利用静态 Create方法创建元组 + /// 最多支持八个元素 + /// </summary> + public Tuple<T1, T2> GetTuple_StaticMethod_Tow<T1, T2>(T1 tupleValue1, T2 tupleValue2) + { + var tuple = Tuple.Create<T1, T2>(tupleValue1,tupleValue2); + + return tuple; + } + + /// <summary> + /// 超过8项,使用嵌套 + /// </summary> + public Tuple<int, int, int, int, int, int, int, Tuple<int, string>> GetTuple_Constructor_Nest() + { + var nestTuple = new Tuple<int, string>(8, "嵌套Tuple"); + var tuple = new Tuple<int, int, int, int, int, int, int, Tuple<int, string>>(1, 2, 3, 4, 5, 6, 7, nestTuple); + + + //此处不能使用Tuple.Create()方法 + return tuple; + } + #endregion + + #region 表示一组数据 + + /// <summary> + /// 返回一组数据 + /// </summary> + public Tuple<int, string, uint> GetStudentTuple(int id=1,string name="wanggaofeng", uint age=18) + { + var tuple = Tuple.Create<int, string, uint>(id, name, age); + return tuple; + } + #endregion + + #region 从方法返回多个值 + + /// <summary> + /// 方法返回多值 + /// </summary> + public Tuple<int, string, int, decimal> GetEgg() + { + var tuple = Tuple.Create<int, string, int, decimal>(1, "白色金蛋", 2018001, new decimal(25.50)); + + return tuple; + } + #endregion + + #region 用于单参数方法的多值传递 + /// <summary> + /// 砸金蛋 + /// 单参数方法的多值传递 + /// </summary> + public bool SmashEgg(object egg) + { + var tuple = egg as Tuple<int, string, int, decimal>; + 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 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>netstandard2.0</TargetFramework> + </PropertyGroup> + +</Project> 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 +{ + /// <summary> + /// 值元组 学习 + /// ValueTuple是C# 7.0的新特性之一,.Net Framework 4.7以上版本可用, 其它版本可以用 nuget 添加System.ValueTuple + /// 值元组也是一种数据结构,用于表示特定数量和元素序列,但是是和元组类不一样的,主要区别如下: + /// 值元组是结构,是值类型,不是类,而元组(Tuple)是类,引用类型; + /// 值元组元素是可变的,不是只读的,也就是说可以改变值元组中的元素值; + /// 值元组的数据成员是字段不是属性。 + /// </summary> + 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<int>(5); + Assert.Equal(5, tuple.Item1); + + var tuple2 = tupleStudy.GetTuple_Constructor_One<string>("wanggaofeng"); + Assert.Equal("wanggaofeng", tuple2.Item1); + + var student = new Student() { Id = 1, Name = "wanggaofeng", Age = 28 }; + + var studentTuple = tupleStudy.GetTuple_Constructor_One <Student>(student); + Assert.Equal(student, studentTuple.Item1); + } + + [Fact] + public void GetTuple_StaticMethod_Tow_Test() + { + var tuple = tupleStudy.GetTuple_StaticMethod_Tow<int,string>(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<string,Student>("静态方法",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<string,int, string, int, decimal>("错误的参数",1, "白色金蛋", 2018001, new decimal(25.50)); + var reslul1 = tupleStudy.SmashEgg(tuple1); + Assert.False(reslul1); + + var tuple2 = new Tuple<int,string,int,decimal>(1, "白色金蛋", 2018001, new decimal(25.50)); + var reslul2 = tupleStudy.SmashEgg(tuple2); + Assert.False(reslul2); + + var tuple3 = new Tuple<int, string, int, decimal>(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 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props')" /> + <Import Project="..\packages\xunit.core.2.3.1\build\xunit.core.props" Condition="Exists('..\packages\xunit.core.2.3.1\build\xunit.core.props')" /> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{D8CBDB9A-3EDA-4A6B-BB13-E7765B63717E}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>TupleStudyTest</RootNamespace> + <AssemblyName>TupleStudyTest</AssemblyName> + <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <NuGetPackageImportStamp> + </NuGetPackageImportStamp> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Xml.Linq" /> + <Reference Include="System.Data.DataSetExtensions" /> + <Reference Include="Microsoft.CSharp" /> + <Reference Include="System.Data" /> + <Reference Include="System.Net.Http" /> + <Reference Include="System.Xml" /> + <Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL"> + <HintPath>..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll</HintPath> + </Reference> + <Reference Include="xunit.assert, Version=2.3.1.3858, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL"> + <HintPath>..\packages\xunit.assert.2.3.1\lib\netstandard1.1\xunit.assert.dll</HintPath> + </Reference> + <Reference Include="xunit.core, Version=2.3.1.3858, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL"> + <HintPath>..\packages\xunit.extensibility.core.2.3.1\lib\netstandard1.1\xunit.core.dll</HintPath> + </Reference> + <Reference Include="xunit.execution.desktop, Version=2.3.1.3858, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL"> + <HintPath>..\packages\xunit.extensibility.execution.2.3.1\lib\net452\xunit.execution.desktop.dll</HintPath> + </Reference> + </ItemGroup> + <ItemGroup> + <Compile Include="ValueTupleStudyTest.cs" /> + <Compile Include="TupleStudyTest.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <Analyzer Include="..\packages\xunit.analyzers.0.7.0\analyzers\dotnet\cs\xunit.analyzers.dll" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\TupleStudy\TupleStudy.csproj"> + <Project>{3bf11d95-1a17-444f-97a8-5536fcc8c015}</Project> + <Name>TupleStudy</Name> + </ProjectReference> + </ItemGroup> + <ItemGroup> + <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('..\packages\xunit.core.2.3.1\build\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.core.2.3.1\build\xunit.core.props'))" /> + <Error Condition="!Exists('..\packages\xunit.core.2.3.1\build\xunit.core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.core.2.3.1\build\xunit.core.targets'))" /> + <Error Condition="!Exists('..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props'))" /> + </Target> + <Import Project="..\packages\xunit.core.2.3.1\build\xunit.core.targets" Condition="Exists('..\packages\xunit.core.2.3.1\build\xunit.core.targets')" /> +</Project> \ 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<ValueTuple>(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 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="xunit" version="2.3.1" targetFramework="net461" /> + <package id="xunit.abstractions" version="2.0.1" targetFramework="net461" /> + <package id="xunit.analyzers" version="0.7.0" targetFramework="net461" /> + <package id="xunit.assert" version="2.3.1" targetFramework="net461" /> + <package id="xunit.core" version="2.3.1" targetFramework="net461" /> + <package id="xunit.extensibility.core" version="2.3.1" targetFramework="net461" /> + <package id="xunit.extensibility.execution" version="2.3.1" targetFramework="net461" /> + <package id="xunit.runner.visualstudio" version="2.3.1" targetFramework="net461" developmentDependency="true" /> +</packages> \ 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