小编Kon*_*osa的帖子

为什么在C#中使用something.Equals(null)时会出现空引用错误

当我尝试检查某些内容是否为空时,我一直收到空引用错误.我有一个名为User的类,我像这样初始化变量indvUser

User indvUser = api.Users.SearchByExternalId(session.UserInfo.UserId.ToString())
                   .Users.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

然后我想检查indvUser是否为null

if (indvUser.Equals(null))
{
    int a = 1;
}
Run Code Online (Sandbox Code Playgroud)

但是在使用时我得到一个空引用错误Equals(null),我不明白.如果它实际上是null,即没有值,不应该Equals(null)返回true?

c# nullreferenceexception

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

Main是'type'但是像'变量'一样使用

基本上,我试图使用从StackOverflow中找到的代码,它与我想要在我的pictureBoxes之间进行的一些colision检测相关联.但是,我遇到一个错误,指出表单名称是一个类型,但像变量一样使用.代码在这里:

foreach (Control PictureBox in Main)
{
    if (Player.Bounds.IntersectsWith(PictureBox.Bounds))
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

在foreach循环中.它强调了Main这个词,说明了这些确切的词:

'Namespace.Main' is a 'type' but is used like a 'variable'
Run Code Online (Sandbox Code Playgroud)

c# variables foreach types

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

为什么 .NET 5 GC 不收集(或至少调用 Finalize)明确取消引用的对象?

我想测试垃圾收集器,但这样做很困难。

我写了以下琐碎的测试代码:

using System;

class Foo
{
   int  i;
        
   public Foo(int v)
   {
      i = v;
      Console.WriteLine($"{i} was born");
   }
   ~Foo()
   {
       Console.WriteLine($"{i} has died");
   }
}

public class Program
{
   [STAThread]
   public static void Main(string[] args)
   {
       Foo n1 = new Foo(1);
       Foo n2 = new Foo(2);
       Foo n3 = new Foo(3);
       Foo n4 = new Foo(4);
            
       Console.WriteLine("built everything");
       GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
       GC.WaitForPendingFinalizers();
       System.Threading.Thread.Sleep(1000);
                        
       n1 = null;
       n2 = null;
       n3 = n4;
            
       Console.WriteLine("deref n1..n3");
       GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, …
Run Code Online (Sandbox Code Playgroud)

.net c# garbage-collection finalizer roslyn

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

Await Task.Run等到完成然后再做一个动作

这是代码,问题是如何跟踪完成后再运行其他内容?尝试将完成的zip复制到存储位置

private async void ZipFolder(string src, string dest, bool delete)  
{
    await Task.Run(() =>
    {
        using (var zipFile = new ZipFile())
        {
            // add content to zip here 
            zipFile.AddDirectory(src);
            zipFile.SaveProgress +=
                (o, args) =>
                {
                    var percentage = (int)(1.0d / args.TotalBytesToTransfer * args.BytesTransferred * 100.0d);
                    // report your progress
                    pbCurrentFile.Dispatcher.Invoke(
                        System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(
                        delegate()
                        {
                            lblCurrentFile.Content = "Compressing " + src;
                            pbCurrentFile.Value = percentage;
                        }
                        ));
                };
            zipFile.Save(dest);

            if(delete)
            {
                Directory.Delete(src, true);
            }

        }

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

c# async-await

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

会话中的对象引用错误

我收到错误:

你调用的对象是空的.

当我尝试获取会话的值时,它会出错.会话的值为null但我已经尝试测试它以检查它是否为null,并且在if语句中它仍然总是因此错误而失败.

string test;
if (Session["CustType"].ToString() != null || 
    Session["CustType"].ToString() != String.Empty)
{
   test = Session["CustType"].ToString();
   txtCustomerType.Text = test;
}
Run Code Online (Sandbox Code Playgroud)

错误发生在此代码的第二行.我只是不知道该怎么办,因为即使我试图检查它是否为空,如果它只是错误我也无法做任何事情.

c# asp.net webforms

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