小编Fab*_*jan的帖子

使用LINQ通过一个查询获取数组中的前五个元素和最后五个元素

我最近被一位同事问到:是否可以通过一个数组中的一个查询获取前五个元素和最后五个元素?

int[] someArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

int[] someArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
var firstFiveResults = someArray.Take(5);
var lastFiveResults = someArray.Skip(someArray.Count() - 5).Take(5);
var result = firstFiveResults;
result = result.Concat(lastFiveResults);
Run Code Online (Sandbox Code Playgroud)

是否可以通过一个查询获取前五个元素和后五个元素?

c# linq arrays

20
推荐指数
3
解决办法
3091
查看次数

关于输入C#

我尝试使用C#DI方法来实现一些东西.以下是我的代码片段.

public interface IMessageService
{
    void Send(string uid, string password);
}

public class MessageService : IMessageService
{
    public void Send(string uid, string password)
    {
    }
}

public class EmailService : IMessageService
{
    public void Send(string uid, string password)
    { 
    }
}
Run Code Online (Sandbox Code Playgroud)

和代码创建一个ServiceLocator:

public static class ServiceLocator
{

    public static object GetService(Type requestedType)
    {
        if (requestedType is IMessageService)
        {
            return new EmailService();
        }
        else
        {
            return null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我用它创建一个测试代码

public class AuthenticationService
{
    private IMessageService msgService;
    public …
Run Code Online (Sandbox Code Playgroud)

c#

9
推荐指数
2
解决办法
489
查看次数

为什么我的"零分治"预防不起作用?

我一直在使用C#编写计算器,遇到了一个我无法解决的问题.

目前,当用户输入除以零的数字时,答案默认为0.00,而应该是无效的.

我不知道为什么,经过一段时间的修修,我无法弄明白.这是相关代码:

private void button1_Click(object sender, EventArgs e)
{
                double number1, number2, ans; // Identify variables as double to account for decimals.
                number1 = Convert.ToDouble(num1.Text); // Convert the contents of the textBox into a double.
                number2 = Convert.ToDouble(num2.Text); // 
                ans = 0.0;
                string symbol = modifier1.Text;

                if (symbol == "/" && number2 == 0) // This part seems to be broken.
                    answer.Text = "Invalid input.";
                else
                    if (symbol == "+")
                        ans = number1 + number2;
                    else if (symbol == …
Run Code Online (Sandbox Code Playgroud)

c#

5
推荐指数
2
解决办法
181
查看次数

检查JValue是否为空

为什么这段代码不运行,我想检查JSON是否包含key的整数PurchasedValue?():

public PropertyInfo(Newtonsoft.Json.Linq.JToken jToken)
{
    this.jToken = jToken;
    int PurchasedValue = (int)(jToken["PurchasedValue"].Value ?? 0);
}
Run Code Online (Sandbox Code Playgroud)

错误是:

Error CS0019: Operator `??' cannot be applied to operands of type `method group' and `int' (CS0019) 
Run Code Online (Sandbox Code Playgroud)

c# json nullable json.net

5
推荐指数
2
解决办法
4729
查看次数

为什么类型是System.Object?

这是一段代码:

Func<dynamic> f1 = new Func<dynamic>(() => 10);
Func<int> f2 = () => 10;

Console.Write(f1.Method.ReturnType);
Console.Write(" and " + f2.Method.ReturnType );
Run Code Online (Sandbox Code Playgroud)

输出: System.Object and System.Int32

我的问题是:

为什么dynamicDLR(用于的类型)推断的类型dynamicSystem.Object和不是System.Int32?这不是很合乎逻辑,因为它还涉及将Int32结构装箱到对象.

据我所知,当我们在这里检查类型时,我们会在执行时动态执行,因为我们使用Reflection.所以DLR当时知道它是int由于某种原因它把它装进Object......为什么?

我试图在SO上找到任何关于它的东西并用Google搜索,但我找不到任何解释这一切的答案.

c# lambda delegates boxing dynamic

3
推荐指数
1
解决办法
103
查看次数

为什么这种方法组转换在C#7.2及更低版本中不明确?

鉴于以下课程:

public class Foo {
    public Foo(int i, double d) {
        Integer = i;
        Double = d;
    }
    public int Integer {get;}
    public double Double {get;}

    private static Random rand = new Random();
    public static Foo CreateRandom() => new Foo(rand.Next(1,101), rand.NextDouble());
}
Run Code Online (Sandbox Code Playgroud)

以及这种用法:

void Main()
{
    var items = Enumerable.Range(0, 50)
                          .Select(_ => Foo.CreateRandom());

    Console.WriteLine(items.Sum(GetInteger)); // Fine
    Console.WriteLine(items.Sum(GetDouble)); // Ambiguous
    Console.WriteLine(items.Sum(x => x.Double)); // Also fine
    Console.WriteLine(items.Sum((Func<Foo,double>)GetDouble)); // Cast required? Why?

    int GetInteger(Foo item) => item.Integer;
    double GetDouble(Foo item) => …
Run Code Online (Sandbox Code Playgroud)

c# c#-7.0 visual-studio-2017 c#-7.2 c#-7.1

3
推荐指数
1
解决办法
142
查看次数

属性get上的null-coalescing运算符

所以,我正在阅读一篇文章,我遇到了一些让我质疑它的代码.

代码如下所示:

private UserService _userService = null;

protected UserService UserService
{
    get { return _userService ?? Request.GetOwinContext().GetUserManager<UserService>(); }
}
Run Code Online (Sandbox Code Playgroud)

对我来说,私有属性看起来总是为null,因此使用null-coalescing运算符是没用的.我想它和这样做很有用:

protected UserService UserService
{
    get { return Request.GetOwinContext().GetUserManager<UserService>(); }
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?虽然我们在讨论这个问题,但为什么这样做:

private UserService _userService = null;
Run Code Online (Sandbox Code Playgroud)

肯定是这样的

private UserService _userService;
Run Code Online (Sandbox Code Playgroud)

请帮我澄清一下我的怀疑:D

c#

2
推荐指数
1
解决办法
936
查看次数