比较C#中的字符串长度

Cra*_*van -1 c# string string-length

我需要制作确定两个更长的单词输入的功能.我曾尝试使用if语句String.Length,但我无法正确使用它.制作这个功能的最佳方法是什么?以下是主程序.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class LongerWord
{
    public static void Main(string[] args) 
    {
        Console.Write("Give 1. word >");
        String word1 = Console.ReadLine();
        Console.Write("Give 2. word >");
        String word2 = Console.ReadLine();
        String longer = LongerString(word1, word2);
        Console.WriteLine("\"" + longer + "\" is longer word");
        Console.ReadKey();
    }
}
Run Code Online (Sandbox Code Playgroud)

ymz*_*ymz 6

这应该是......的一个开始......

private string LongerString(string x, string y)
{
    return x.Length > y.Length ? x : y;
}
Run Code Online (Sandbox Code Playgroud)

  • @trailmax从这个问题来看,似乎OP在尝试完成任务之前做了他的尽职调查,然后才进入SO.因此,这个问题是完全可以接受的. (2认同)