【问题标题】:c# IDE0090 'new' expression can be simplified visual studio 2022c# IDE0090 \'new\' 表达式可简化visual studio 2022
【发布时间】:2022-11-11 15:23:36
【问题描述】:

我正在适应this snmp program from a user here on stackoverflow。 我在 Visual Studio 2022 中创建了一个新项目,添加了所有依赖项。

当我编译时,我收到 6 条 IDE0090 消息,但我看不出问题出在哪里。我已将 LangVersion 设置为 10。

https://i.stack.imgur.com/BF5P1.jpg

任何人都可以看到问题是什么?

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using SnmpSharpNet;

namespace Exemple2
{
    class Program
    {
        static void Main(string[] args)
        {
            /* Get an SNMP Object
             */
            SimpleSnmp snmpVerb = new SimpleSnmp("192.168.1.121", 161, "public");
            if (!snmpVerb.Valid)
            {
                Console.WriteLine("Seems that IP or comunauty is not cool");
                return;
            }


            /* Sample of simple Get usage on ifSpeed on interface 10
             * TODO : for sure you have to detect the right interface
             */
            Oid oidifSpeed = new Oid(".1.3.6.1.2.1.2.2.1.5.10");

            /* Getting ifSpeed
             */
            Dictionary<Oid, AsnType> snmpDataS = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifSpeed.ToString() });
            if (snmpDataS != null)
                Console.WriteLine("Interface speed \"{0}\" : {1}", oidifSpeed.ToString(), snmpDataS[oidifSpeed].ToString());
            else
                Console.WriteLine("Not Glop!");

            /* Sample of simple Get usage on ifInOctets on interface 10
             * TODO : for sure you have to detect the right interface
             */
            Oid oidifInOctets = new Oid(".1.3.6.1.2.1.2.2.1.10.10");
            Dictionary<Oid, AsnType> snmpData1;

            /* Getting it for the first time
             */
            snmpData1 = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifInOctets.ToString() });
            if (snmpData1 != null)
                Console.WriteLine("Number of In octets \"{0}\" : {1}", oidifInOctets.ToString(), snmpData1[oidifInOctets].ToString());
            else
                Console.WriteLine("Not Glop!");

            int missed = 0;
            while (true)
            {
                if (missed == 0)
                {
                    /* When you detect a non refesh data, keep the last one
                     */
                    snmpData1 = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifInOctets.ToString() });
                    if (snmpData1 != null)
                        Console.WriteLine("Number of In octets \"{0}\" : {1}", oidifInOctets.ToString(), snmpData1[oidifInOctets].ToString());
                    else
                        Console.WriteLine("Not Glop!");
                }

                /* Some Wait (less aproximative)
                 */
                int duration = 5;
                System.Threading.Thread.Sleep(duration * 1000); // duration seconds

                /* Getting it for the Second time
                 */
                Dictionary<Oid, AsnType> snmpData2 = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifInOctets.ToString() });
                if (snmpData2 != null)
                    Console.WriteLine("Number of In octets \"{0}\" : {1}", oidifInOctets.ToString(), snmpData2[oidifInOctets].ToString());
                else
                    Console.WriteLine("Not Glop!");

                Counter32 I1 = new Counter32();
                I1.Set(snmpData1[oidifInOctets]);
                Counter32 I2 = new Counter32();
                I2.Set(snmpData2[oidifInOctets]);
                Counter32 speed = new Counter32();
                speed.Set(snmpDataS[oidifSpeed]);

                if (I2.Value == I1.Value)
                {
                    missed += 1;
                    continue;
                }
                decimal bandWithUsage = (((decimal)(I2.Value - I1.Value) * 8) * 100) / (speed * duration * (1 + missed));
                Console.WriteLine("BandWith usage : {0}%", bandWithUsage);
                missed = 0;
            }
        }
    }
}

【问题讨论】:

  • 这些是信息......它们甚至不是警告,更不用说错误了。该程序确实编译了。
  • 我不赞成这个问题,因为它显然没有显示任何研究工作。亲爱的 OP,您是否尝试过 Google “IDE0090”?就算打我感觉我是幸运的,你会知道它是关于什么的。

标签: c# visual-studio


【解决方案1】:

编译器建议因为您明确指定字段类型而不是使用 var snmpVerb = ... ,所以您不需要构造函数名称并使用以下缩短的语法:

SimpleSnmp  snmpVerb = new ("192.168.1.121", 161, "public");

如果您右键单击对SimnpleSnmp 构造函数的调用并选择快速操作和重构,它将为您提供一些建议,这就是其中之一。

【讨论】:

    【解决方案2】:

    正如IDE0090 docs 所示,您可以使用C# 9 target-typed new expressions 并简化实例创建。例如来自:

    SimpleSnmp snmpVerb = new SimpleSnmp("192.168.1.121", 161, "public");
    Oid oidifSpeed = new Oid(".1.3.6.1.2.1.2.2.1.5.10");
    

    至:

    SimpleSnmp snmpVerb = new("192.168.1.121", 161, "public");
    Oid oidifSpeed = new(".1.3.6.1.2.1.2.2.1.5.10");
    

    或者只使用var

    var snmpVerb = new SimpleSnmp("192.168.1.121", 161, "public");
    var oidifSpeed = new Oid(".1.3.6.1.2.1.2.2.1.5.10");
    

    请注意,这些不是错误,而是信息性消息,它们不会影响程序的正确性/编译。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-05
      • 2020-05-14
      • 1970-01-01
      • 2021-12-28
      • 2021-12-23
      • 2023-01-02
      • 2022-09-25
      • 2021-10-04
      相关资源
      最近更新 更多