小编mjw*_*lls的帖子

异步方法不识别收益率返回方法?

问题

当我尝试在async方法中调用我的“ normal”方法时,它将从Debugger 1中被忽略。

这是我的异步方法

 internal async static Task<DefinitionsModel> DeserializeAsync(this string path)
 {
        var model = new DefinitionsModel();
        var content = await File.ReadAllTextAsync(path);

        model.Pages = content.GetPages();

        return model;
 }
Run Code Online (Sandbox Code Playgroud)

这是我的“正常”方法

private static IEnumerable<PageModel> GetPages(this string content)
{            
        var level = 0;
        var value = nameof(PageModel.Page).GetDElement<PageModel>();
        var start_with_line = $"{level} {value} ";
        var end_with_line = string.Concat(Enumerable.Repeat(Environment.NewLine, 2));

        var expression = $@"\b{start_with_line}\S * {end_with_line}\b";
        var matches = content.GetPagesFromContent(expression);


        yield return new PageModel();
}
Run Code Online (Sandbox Code Playgroud)

帮助图片

图片链接

c# yield

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

错误 CS0117(“调试”不包含“日志”的定义)

当一个游戏对象与另一个有碰撞的游戏对象发生碰撞时,我试图在控制台上显示。我一直在统一的控制台错误 CS0117 上收到此错误,“调试”不包含“日志”的定义。

  • 我在 mac 上使用 .net core
  • 我使用 vs 代码 1.35.1 和统一 2019.3.0a5
  • 我已经拥有了我需要的所有用途,但是智能感知没有为我的调试或任何东西找到定义,这令人沮丧:/
  • 我也没有任何其他名为 debug 的 .cs 文件

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine.SceneManagement;
    
    public class DeadZone : MonoBehaviour
    {
        private void OnCollisionEnter2D(Collision2D collision){
            Debug.log("Collision");
        }
    
        private void OnTriggerEnter2D(Collider2D collision){
            Debug.log("Trigger");
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

当我的球游戏对象接触墙壁游戏对象时,我希望统一控制台中出现“碰撞”消息,两者都带有碰撞器,但我只在控制台中收到该错误,我也已经尝试过使用 UnityEngine.Debug.log(); 但还没有取得任何成功... :(

c# debugging unity-game-engine visual-studio-code

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

在视图中允许可空模型

使用/或任何其他类型作为我的 MVC 模型时,编译器似乎显示错误。structenum

CS0037 无法将 null 转换为“MyEnum”,因为它是不可为 null 的值类型

我创建了一个新的 MVC 项目(在 VS 2019 中),创建了一个枚举

public enum MyEnum
{
    One,
    Two,
    Three
}
Run Code Online (Sandbox Code Playgroud)

并将其作为模型放在“关于”视图中:

在此处输入图片说明

为什么会发生这种情况?

c# asp.net-mvc nullable visual-studio visual-studio-2019

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

对象null和空检查的最佳方法是什么?

我在C#中有如下代码:

If  (this._university != Null and this._university.department !=null &&
     this._university.department.class !=null && 
     this._university.department.class.student != null && 
     this._university.department.class.student.name != null &&
     this._university.department.class.student.name.firstname != null &&
     this._university.department.class.student.name.firstname !=String.empty)

{
   // selecting first letter of Firstname
   var F_firstname = this._university.department.class.student.name.firstname[0].tostring();
}
Run Code Online (Sandbox Code Playgroud)

但是代码看起来非常糟糕,无法检查null对象.我们有更好的方法来检查对象的空值吗?

c#

0
推荐指数
1
解决办法
1206
查看次数

为什么LINQ不确定?

我随机排序了一个IEnumerable.我继续打印出相同的元素,并获得不同的结果.

string[] collection = {"Zero", "One", "Two", "Three", "Four"};
var random = new Random();
var enumerableCollection = collection.OrderBy(e => random.NextDouble());

Console.WriteLine(enumerableCollection.ElementAt(0));
Console.WriteLine(enumerableCollection.ElementAt(0));
Console.WriteLine(enumerableCollection.ElementAt(0));
Console.WriteLine(enumerableCollection.ElementAt(0));
Console.WriteLine(enumerableCollection.ElementAt(0));
Run Code Online (Sandbox Code Playgroud)

每次写入都会给出不同的随机元素.为什么订单没有保留?

在.NET Fiddle上看到它

c# linq ienumerable non-deterministic deferred-execution

0
推荐指数
1
解决办法
168
查看次数

.NET 传递引用

我有这个项目的依赖:

Client[.NET 4.7.2] -> Infrastructure[.NET Standard 2.0] -> ExternalLib[dll]

当我尝试从项目中调用该方法时Infrastructure,编译器生成以下错误:ExternalLibClient

错误 CS0012 该类型ClassX是在未引用的程序集中定义的。您必须添加对程序集的引用ExternalLib

是否可以隐式地做到这一点?我的意思是不需要显式添加这个引用,因为它已经在Infrastructure项目中完成了。

谢谢

.net c#

0
推荐指数
1
解决办法
600
查看次数

如何检查数字数组中出现的具体数字

我想检查一组数字中出现的具体数字.例如,数字2出现:

input from 1 to 20 
output
3 times 
Run Code Online (Sandbox Code Playgroud)

另一个例子:

input from 1 to 100
output 
19 times 
Run Code Online (Sandbox Code Playgroud)

这是我的代码

int count = 0;
string x = "";
string y = "";
string[] arr2 = new string[100000000];
for (int i = 1; i < arr2.Length; i++)
{
    arr2[i - 1] = i.ToString();
}

foreach (var item in arr2)
{  
    for (int digit = 0;digit<item.Length;digit++)
    {
        if (digit == 2)
            count++;
    }
}
Console.WriteLine(count);
Run Code Online (Sandbox Code Playgroud)

我的代码不起作用,但我不知道问题出在哪里.

注意:这必须仅使用for/ foreachloops,而不是使用Dictionary或LINQ或 …

c# arrays

-1
推荐指数
1
解决办法
84
查看次数

尝试输出到textBox.Text时键入转换问题

我有下面的代码,给我以下错误:

System.InvalidCastException:'无法将
'System.Windows.Forms.TextBox'类型的对象强制转换为'System.IConvertible'.'

textBox1.Text = Math.Sqrt(10.0 * (Convert.ToInt32(textBox2Value)) / 
    (Convert.ToInt32(textBox3Value))).ToString();
Run Code Online (Sandbox Code Playgroud)

文本框2(命名textBox2Value)和3(命名textBox3Value)值是整数,我假设使用ToString()将其评估为textBox1.Text就足够了,但事实并非如此.我不知道为什么.我尝试将textBox值转换为Doubles或Floats,但它没有区别所以我认为它不是公式本身,而是将该值显示为字符串?

任何人都可以协助我出错的地方吗?

c# winforms

-1
推荐指数
1
解决办法
29
查看次数

String.IndexOf()返回字符串的意外索引

String.IndexOf()方法不符合我的预期。

我希望它不会找到匹配项,因为所输入的确切单词不在str

string str = "I am your Friend";
int index = str.IndexOf("you",0,StringComparison.OrdinalIgnoreCase);
Console.WriteLine(index);
Run Code Online (Sandbox Code Playgroud)

输出5

我的预期结果是-1,因为字符串不包含

.net c# indexof

-1
推荐指数
1
解决办法
110
查看次数

为什么我的结果没有像我期望的那样写入控制台?

我正在编写一个程序,可以让您输入两个数字并为其提供所有数学答案。因此像乘法,除法等。问题是当我运行它时,它要求输入,然后只显示所有运算符字符串,但为空,然后在此结束。

我试着制作一个数组,然后将其设置为等于Console.WriteLines中的运算符,但这没有用。

static void Main(string[] args)
{
    Console.WriteLine("Input any two numbers.");

    var number1 = int.Parse(Console.ReadLine());
    var number2 = int.Parse(Console.ReadLine());

    Console.WriteLine("Addition: ", + (number1 + number2));
    Console.WriteLine("Division: ", + (number1 / number2));
    Console.WriteLine("Subtraction: ", + (number1 - number2));
    Console.WriteLine("Multiplication: ", + (number1 * number2));
}
Run Code Online (Sandbox Code Playgroud)

c#

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

线程安全阅读Guid Twice

我正在读一个Guid对象两次,一次是在if语句中,一次是在if语句的块中.

在CI中,将在本地复制变量,以确保if语句中的读取操作不会读取不同的值(如果另一个线程在中间更改此值).

我怀疑以下方法是a)线程安全和b)会给我相同的值:

public class MyClass {

private Guid MyProp {get;set;} = Guid.Empty; //May be changed at will

public OnRunLoop() //Gets called periodicaly on a thread
{

    var ALocalCopyOfTheProp = MyProp; //Copy locally
    if(ALocalCopyOfTheProp == AValueFromAnotherPlace) //Read 1
    {
        ...

        var AnotherReadOfTheVariable = ALocalCopyOfTheProp; //Read 2

    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

C#.NET本身没有谷歌搜索的复制功能,所以在这种情况下最佳做法是什么?

编辑: - 请注意,在这种情况下,MyProp不在我的手中.我不能使用锁,因为该物业来自其他地方.(道歉没有让它更清晰) - Guid是一个结构而不是引用类型

.net c# guid

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

字符串的长度不是我期望的

下面的方法产生意外的输出:

public static void Main(string[] args)
    {
        var dict = new Dictionary<int, string>()
        {
            {       2, "0000 0000 0000 0000 0000 0000 0000 0010".Replace(" ", "") },
            {       0, "0000 0000 0000 0000 0000 0000 0000 0000".Replace(" ", "") },
            { 7529548, "0000 0000 ?0111 0010 1110 0100 0100 1100?".Replace(" ", "") }
        };

        foreach(var pair in dict)
        {
            Console.WriteLine(pair.Value.Length);
        }
        Console.ReadKey();
    }
Run Code Online (Sandbox Code Playgroud)

输出为: 32、32、34

如果我使用编译器设置,它也不会改变。

为什么会发生这种情况?为什么在最后一种情况下字符串长度可能不同?

c# string dictionary

-4
推荐指数
1
解决办法
54
查看次数