简体   繁体   English

使用指令是不必要的Visual Studio-随机类

[英]using directive is unnecessary visual studio - random class

I want to use Random class but I got this error message: 我想使用随机类,但收到以下错误消息:

using directive is unnecessary visual studio

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Random; //I got this error at this line

namespace Mikrotik
{
....

Quick-fix: 快速解决:

You only need using System; 您只需要using System; - inside your code you can use Random directly. -在您的代码中,您可以直接使用Random

Background: 背景:

Please read the documentation for C#'s using directive: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive 请阅读有关C#的using指令的文档: https : //docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/using-directive

I think you're imagining that using works like how import works in Java where you have to name each type you want to bring into scope or use .* to bring all types into scope. 我想您正在想象using诸如Java中import工作方式这样的工作,您必须命名要引入作用域的每种类型,或使用.*将所有类型引入作用域。 Instead, C#'s using only imports namespaces, making it roughly equivalent to Java's import being used exclusively with the .* syntax. 相反,C#仅using import命名空间,使其大致等同于Java的import仅与.*语法一起使用。

You're getting the error because there is no namespace System.Random , instead it's a type-name; 因为没有命名空间System.Random ,所以它得到一个错误,它是一个类型名称。 so you just need using System; 所以你只需要using System; - in your code you can use Random as-is. -在您的代码中,您可以按原样使用Random

For pendantry: C#'s using directive has 3 different modes: 对于垂饰:C#的using指令具有3种不同的模式:

  1. using namespace; - brings into scope all types in the specified namespace -将指定名称空间中的所有类型纳入范围
  2. using Alias = namespace.TypeName; - brings into scope a single type, but with an alias. -将单一类型(但具有别名)纳入范围。 The alias can match the original name. 别名可以与原始名称匹配。
  3. using static namespace.TypeName; - brings only the static members of the specified type into scope. -仅将指定类型的静态成员纳入范围。

Not to be confused with C#s using() {} statement, which is completely unrelated to importing types and namespaces. 不要与C#的using() {}语句混淆,这与导入类型和名称空间完全无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是什么使using指令不必要? - What makes a using directive unnecessary? Visual Studio 2013,JavaScriptExecutor使用指令 - Visual Studio 2013, JavaScriptExecutor using directive 使用预处理程序指令#if在Visual Studio中调试WindowsService - Debug WindowsService in Visual Studio using preprocessor directive #if 如何在vscode中禁用不必要的指令使用警告? - How to disable the warning of Unnecessary Using of Directive in vscode? 在Visual Studio 2010中显示不必要的用法 - Show unnecessary usings in Visual studio 2010 从完全限定的类名生成“using”指令,而不必转到Visual Studio 2017中的代码文件的顶部 - Generate “using” directive from fully-qualified class name without having to go to the top of the code file in Visual Studio 2017 Visual Studio 2013 中的“您是否缺少 using 指令或程序集引用” - “Are you missing a using directive or an assembly reference” in visual studio 2013 Using 指令在 Visual Studio 2015 C# 中不起作用 - Using directive does not work in Visual Studio 2015 C# Visual Studio 2010 - 您是否缺少using指令或程序集引用? - Visual Studio 2010 — are you missing a using directive or an assembly reference? Visual Studio C#:为什么某些类库的“ using”指令不足? - Visual Studio C#: Why is a “using” directive insufficient for some libraries?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM