zoukankan      html  css  js  c++  java
  • 返回元组

    返回元组


    首先,我们讨论为什么应该避免使用元组。假设函数返回元组,用户就必须引用  FSharp.Core.dll;另外,须要使用元组的代码C# 中看并不好。

    考虑以下的样例,我们定义了函数 hourAndMinute,它从结构 DateTime 中返回时、分。




    #light
    module Strangelights.DemoModule
    open System


    // returns the hour and minute from the give date as a tuple
    let hourAndMinute (time: DateTime) = time.Hour, time.Minute


    // returns the hour from the given date
    let hour (time: DateTime) = time.Hour
    // returns the minutes from the given date
    let minute (time: DateTime) = time.Minute


    为了从 C# 中调用这个函数。还要跟着完毕以下的样例。

    假设你使用 Visual Studio。须要在 F# 解决方式中创建一个 C# 项目,即,选择 文件-加入-新建项目,再选择 C# 控制台项目,如图 14-1 所看到的。


     
    图 14-1 怎样新建 C# 项目
    接下来,须要在 C# 项目中加入对 F# 项目的引用,然后,加入以下的 C# 类到刚创建的项目中。


    // !!! C# Source !!!
    using System;
    using Strangelights;
    using Microsoft.FSharp.Core;


    static class PrintClass {
      internal static void HourMinute() {
        // call the "hourAndMinute" function and collect the
        // tuple that's returned
        Tuple<int, int> t = DemoModule.hourAndMinute(DateTime.Now);
        // print the tuple's contents
        Console.WriteLine("Hour {0} Minute {1}", t.Item1, t.Item2);
      }
    }


    演示样例的执行结果例如以下:


    Hour 16 Minute 1


    尽管前面演示样例中的 C# 并不太难看,可是,假设把这个函数分成两个,一个返回小时。一个返回分钟。可能会更好。

  • 相关阅读:
    servlet的方法解析
    jsp九大内置对象之一request
    java 线程的简单理解
    《你的灯亮着吗》阅读笔记二
    《你的灯亮着吗》阅读笔记一
    第二段冲刺进程1
    对“搜狗输入法”的评价
    “找一”分析报告
    “找出水王”分析报告
    “买书方案”分析报告
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/7068585.html
Copyright © 2011-2022 走看看