[英]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
{
....
You only need using System;
您只需要using System;
- inside your code you can use Random
directly. -在您的代码中,您可以直接使用Random
。
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种不同的模式:
using namespace;
- brings into scope all types in the specified namespace -将指定名称空间中的所有类型纳入范围 using Alias = namespace.TypeName;
- brings into scope a single type, but with an alias. -将单一类型(但具有别名)纳入范围。 The alias can match the original name. 别名可以与原始名称匹配。 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.