首页 > 其他分享 >CSharp: Tuples

CSharp: Tuples

时间:2024-05-22 20:09:24浏览次数:19  
标签:Console name dynamicResult Tuples WriteLine CSharp var id

 

        /// <summary>
        /// 使用元组(Tuples或ValueTuple
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static (int, string) GetInfo(int gid,string gname)
        {
            int id = gid;
            string name = gname;
            return (id, name);
        }
        /// <summary>
        /// AnonymousType
        /// </summary>
        /// <returns></returns>
        public static dynamic GetValuesAsDynamic(int gid, string gname)
        {
            return new { id = gid, name =gname };
        }

  

//元组Tuples
var person = ("Geovin Du", 30, "Soft Developer");
Console.WriteLine(person.Item1);

Tuple<string, int, string> persons = new Tuple<string, int, string>("Geovin Du", 30, "Soft Developer");
Console.WriteLine($"Name: {persons.Item1}, Age: {persons.Item2}, Occupation: {persons.Item3}");

//使用元组(Tuples或ValueTuple
var (gid, gname) = SortExample.GetInfo(20, "geovin");
Console.WriteLine($" id:{gid},name:{gname}");

dynamic dynamicResult=SortExample.GetValuesAsDynamic(20, "geovindu");
Console.WriteLine(dynamicResult);
Console.WriteLine(dynamicResult.GetType().GetProperty("id").GetValue(dynamicResult, null));
Console.WriteLine(dynamicResult.GetType().GetProperty("name").GetValue(dynamicResult, null));
Console.WriteLine(dynamicResult.GetType());

Stopwatch stopwatch = Stopwatch.StartNew();

Console.WriteLine(typeof(Person));
foreach (var propertyInfo in typeof(Person).GetProperties())
{
    Console.WriteLine(propertyInfo.Name);
}


var school = new
{
    Address = "Orlando",
    Contact = 1200,
    Employee = new { Id = 3, Name = "Tina" }
};

Console.WriteLine(school.Address);


dynamicResult = SortExample.GetValuesAsDynamic(30, "geovindu");

//AnonymousType another = new AnonymousType(something);
if (dynamicResult!=null)
{

    var v = dynamicResult;
    // v.id = 3;
    // v.name = "geovindu";
    Console.WriteLine(v);
   // object o = (object)v;
    //Console.WriteLine(o.ToString());

    //var d= Newtonsoft.Json.JsonConvert.DeserializeObject(dynamicResult.ToString());
    var d = JsonConvert.DeserializeObject<dynamic>("{ id: 30, name: \"geovindu\" }");


}

    stopwatch.Stop();
Console.WriteLine("Delegate: {0}ms", stopwatch.ElapsedMilliseconds);

  

标签:Console,name,dynamicResult,Tuples,WriteLine,CSharp,var,id
From: https://www.cnblogs.com/geovindu/p/18206972

相关文章

  • CSharp: SunCalc for calculating
     usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Threading.Tasks;namespaceWebAppPdfDemo{///<summary>//////</summary>publicstaticclassDateTimeJavaScriptExt{......
  • CSharp: SunTimeCalculator
     usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;namespaceWebAppPdfDemo{///<summary>//////</summary>publicclassSunTimeCalculator{#region辅助函数///<......
  • 【YoloDeployCsharp】基于.NET Framework的YOLO深度学习模型部署测试平台
    1.项目介绍  基于.NETFramework4.8开发的深度学习模型部署测试平台,提供了YOLO框架的主流系列模型,包括YOLOv8~v9,以及其系列下的Det、Seg、Pose、Obb、Cls等应用场景,同时支持图像与视频检测。模型部署引擎使用的是OpenVINO™、TensorRT、ONNXruntime以及OpenCVDNN,支持CP......
  • csharp 获取当前程序的所在目录
    获取当前程序所在的目录//获取当前程序的执行目录信息Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);//asp.net使用,不过在console下也是/xxx/bin/Debug/net8.0/Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().Location);//=>/xxx/......
  • csharp selenium HtmlAgilityPack 爬虫 网页解析 微信公众号
    Wechat.Crawler/App/App.csproj<ProjectSdk="Microsoft.NET.Sdk"><ItemGroup><ProjectReferenceInclude="..\Blog\Blog.csproj"/></ItemGroup><ItemGroup><NoneUpdate="nlog.config&......
  • Csharp线程
    CSharpe线程 目录CSharpe线程C#如何操作线程Thread1.Thread如何开启一个线程呢?2.Thread中常见的API3.thread的扩展封装threadpool一、.NETFramework2.0时代:出现了一个线程池ThreadPool二、线程池如何申请一个线程呢?三、线程等待四、线程池如何控制线......
  • Csharp中表达式树
    Csharper中的表达式树这节课来了解一下表示式树是什么?在C#中,表达式树是一种数据结构,它可以表示一些代码块,如Lambda表达式或查询表达式。表达式树使你能够查看和操作数据,就像你可以查看和操作代码一样。它们通常用于创建动态查询和解析表达式。一、认识表达式树为什么要这样说......
  • CSharp: ImageToText using Microsoft.SemanticKernel
     usingMicrosoft.SemanticKernel.ImageToText;usingMicrosoft.SemanticKernel;usingMicrosoft.SemanticKernel.Connectors.OpenAI;usingMicrosoft.SemanticKernel.Connectors.HuggingFace;//usingMicrosoft.SemanticKernel.Orchestration;usingMicrosoft.Semanti......
  • CSharp: Tuples
     //元组Tuplesvarperson=("GeovinDu",30,"SoftDeveloper");Console.WriteLine(person.Item1);Tuple<string,int,string>persons=newTuple<string,int,string>("GeovinDu",30,"SoftDeveloper");C......
  • C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压功能
    原文链接:https://www.jb51.net/article/131706.htm网上大部分都挺复杂usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;usingICSharpCode.SharpZipLib.Zip;usingICSharpCode.SharpZipLib.Checksums;usingSystem......