一行数组的 console.writeline 值

我想扩展UserManager以允许通过用户名查找用户。我创建了一个扩展方法。我注意到实现它的最简单方法是获取UserStore. 我不能这样做,UserManager因为Store受到保护。我决定使用 DI 注入UserStore我的方法,但我什至不确定它是否可能。


我试图作为第二个参数通过,IUserStore<T> store但 VS 说没有给出任何参数。


using System.Threading;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Identity;


namespace Learning.Extensions

{

    public static class UserManagerExtension

    {

        public static Task<T> FindByUserNameAsync<T>(this UserManager<T> userManager, IUserStore<T> store, string userName) where T : class

        {

            var result = store.FindByNameAsync(userName, CancellationToken.None);


            return result;

        }

    }

}


手掌心
浏览 133回答 4
4回答

潇湘沐

无需创建新数组,只需连接两个数组,然后使用Write而不是WriteLine:foreach(var res in arr1.Concat(arr2){&nbsp; Console.Write($"{res} ");}

呼啦一阵风

Write而不是WriteLine, 加上使用LINQ更少的代码:&nbsp; &nbsp; &nbsp; &nbsp; int[] arr1 = { 37, 45, 65 };&nbsp; &nbsp; &nbsp; &nbsp; int[] arr2 = { 70, 89, 118 };&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Combined array elements..");&nbsp; &nbsp; &nbsp; &nbsp; arr1.Concat(arr2).ToList().ForEach(x => Console.Write($"{x} "))

DIEA

int[] arr1 = { 37, 45, 65 };&nbsp; &nbsp; &nbsp; &nbsp; int[] arr2 = { 70, 89, 118 };&nbsp; &nbsp; &nbsp; &nbsp; var myList = new List<int>();&nbsp; &nbsp; &nbsp; &nbsp; myList.AddRange(arr1);&nbsp; &nbsp; &nbsp; &nbsp; myList.AddRange(arr2);&nbsp; &nbsp; &nbsp; &nbsp; int[] arr3 = myList.ToArray();&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Combined array elements..");&nbsp; &nbsp; &nbsp; &nbsp; foreach (int res in arr3)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write(" " + res + " ");&nbsp; &nbsp; &nbsp; &nbsp; }

喵喵时光机

只需使用Console.Write或string str;foreach (int res in arr3){&nbsp; &nbsp; str += $"{res} ";}Console.WriteLine(str);在这种情况下,如果数组很大,你应该使用StringBuilder但在我看来,最好的方法是这样的:int[] arr1 = { 37, 45, 65 };int[] arr2 = { 70, 89, 118 };Console.Write(String.Join(" ", arr1.Concat(arr2)));
打开App,查看更多内容
随时随地看视频慕课网APP