Namespace system c что это
Перейти к содержимому

Namespace system c что это

  • автор:

What does "using System" mean in C#? [closed]

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 2 years ago .

["using System"] mean? Why can’t you start your code without these lines?

5 Answers 5

The using System line means that you are using the System library in your project. Which gives you some useful classes like Console or functions/methods like WriteLine .

The namespace ProjectName is something that identifies and encapsulates your code within that namespace. It’s like packages in Java. This is handy for organizing your codes.

class Program is the name of your entry-point class. Unlike Java, which requires you to name it Main , you can name it whatever in C#.

And the static void Main(string[] args) is the entry-point method of your program. This method gets called before anything in your program.

And you can actually write a program without some of them in DotNet 5 and later as they now support top-level functions.

C# Using System

These are separate from the language-level aliases in the C# language. System can be referenced with different syntax forms. It is usually specified with a using System directive.

Example. We look at three different representations of the same program. The first version uses the System.Console type—it omits the "using System" directive. It also uses the int alias in the C# language.

The second version changes the 'int' alias to the 'System.Int32' type found in the System alias. The C# language automatically does this. It is typically best to use the 'int' keyword for readability.

The third version adds the 'using System' directive at the top. Now, the Console type can be accessed directly. Also, Int32 can be accessed directly because it too is located in System.

Tip: You can fix several common errors by adding the System namespace to your program.

And: If there are any calls to Console.WriteLine, the System namespace is required. Many other common functions require System as well.

Discussion. The C# language uses aliased keywords that map to types. For example, if you use the keyword int, you are actually using System.Int32. However, because "int" is at the language level, you do not need to include System to use it.

Further, there are many namespaces in the .NET Framework, such as System.IO, that are sub-namespaces to System. These are not automatically included with System. You must include System.IO to get the input-output namespace as well.

Tip: Another root namespace available in Visual Studio is the Microsoft namespace.

https://amdy.su/wp-admin/options-general.php?page=ad-inserter.php#tab-8

And: This contains some useful types, such as those required to do Microsoft Office interoperation.

Summary. System can be understood as a commonly-used subset of the types in the .NET Framework. It is not part of the C# language exactly, but rather it includes types that are mapped to by the aliased keywords in the C# language such as 'int'.

Namespaces

We have used namespaces in our exercises and examples since the beginning of the course. But what are they, exactly?

Namespaces have two main functions in C# programming. First, they are used to organize classes into coherent groups. Second, they are used to control the scope and visibility of class and method names in large programs.

Using namespaces

The most common namespace so far, is the System namespace. In our programs, we call it as follows:

With this using directive we tell our program, it should have access to the System namespace, and it can use classes, methods, and even nested namespaces, from System. For example, when we print in our code something to the console:

We are actually calling:

With the using System, we don’t have to write the long form of the classes (Console) and methods (WriteLine) called, so we can keep our code cleaner and shorter. Or how do you feel, creating for example a list every time with…

Scoping with namespaces

The keyword namespace can be used to declare a scope for classes and methods within your project. This is crucial in especially large projects, but can be used to organize projects of any size. Let’s look at an example:

What just happened?

First, we create a namespace called OuterNamespace. This namespace contains all the code in our example namespace.

We define using System to be available for all the classes in that namespace. Even though we have nested namespaces and multiple classes, System is available for all of them.

We define a class Example, and for it a method ExampleMethod, which prints an informative text.

Next, we define a namespace inside a namespace, or a nested namespace.

Inside this namespace we have a class called Example. As it is defined inside the nested namespace, it can have the same name, as the class in the outer namespace. In the class, we define a method to print information.

In our Main, we call our classes and their methods. Notice, that we are not using “using”, but the long names of the namespaces. Also, the Main is in different namespace.

Let’s move our Main to the same namespace as the classes:

As you can see, the code is already quite much clearer, as the Main is now in the same scope, or in the same namespace as the classes: The Main can “see” the classes and methods inside the OuterNamespace directly, as it is also part of it.

Hierarchy and fully qualified names

Namespaces and Types have fully qualified names, or unique title, which indicate the hierarchy of said items. In our example above, we use the statement OuterNamespace.InnerNamespace.Example. This shows us a hierarchy structure:

  • OuterNamespace is the name of the outmost namespace (or type, this time namespace)
    • InnerNamespace is the name of the second namespace (or type)
      • Example is nested inside the namespace above
        • Etc, etc, etc…

        So, with the . (dot) we indicate a hierachy of items. Let’s look at another example:

        In the example above:

        • The namespace Outmost has a fully qualified name Outmost.
        • The namespace Inner is a member of Outmost, and has a fully qualified name Outmost.Inner.
        • The class ExampleClass is a member of Outmost, and has fully qualified name Outmost.ExampleClass
        • The class name ExampleInnerClass can be seen twice in our code. As they are declared in different unique locations, they have unique fully qualified names:
          • The first one is Outmost.ExampleClass.ExampleInnerClass
          • The second one Outmost.Inner.ExampleInnerClass

          Just by looking at the fully qualified names, we can know if a type or a namespace is part of a larger whole, and how the structure above has been created.

          Copyright © 2020 Heikki Ahonen. More information about licensing here.

          Namespaces

          Namespaces are groupings of similar classes. We don’t want to go into a lot of detail about namespaces in this introductory course, but almost every C# program uses them, so in this video, we’re going to show you the bare minimum you need to know.

          • Teacher’s Notes
          • Questions?
          • Video Transcript
          • Downloads
          • Workspaces

          Resources

          • Unit Testing in C# – Treehouse course
          • Unit testing basics – Visual Studio Docs

          Here’s a program that picks a random floating-point number between 0 and 1, rounds it to a single decimal place, and prints it.

          Don’t worry too much about the details of how this program works. But notice that like the programs we’ve already shown you, many of the types in this program begin with System , followed by a dot. System is used so frequently that writing it over and over starts to make the code look cluttered.

          We write System so frequently because we’re using types from the System namespace. Namespaces are groupings of similar classes. We don’t want to go into a lot of detail about namespaces in this introductory course, but almost every C# program uses them, so in this video, we’re going to show you the bare minimum you need to know.

          Here’s another program, a unit test. We can run it from the command line with dotnet test .

          Again, don’t worry too much about the details of how this works. We’ll have a link in the teacher’s notes if you want to learn more about unit testing [TODO]. What we want you to notice right now is that Microsoft.VisualStudio.TestTools.UnitTesting is repeated several times throughout the code. It’s another namespace.

          Because writing the name of a namespace over and over can clutter your code, C# offers another shortcut. The using directive lets you access types in a namespace without having to prepend the namespace onto the type name.

          • Let me go back to the first program I showed you.
          • At the top of the file, I’ll type the keyword using , a space, and the name of the namespace I want to use, which is System . And as always, each statement in a C# program needs to end with a semicolon: using System;
          • That’s how you write a using directive.
          • Now, if I save my work, I can still run the program without making any further changes: dotnet run
          • But now that I have the using System directive at the top of my file, I don’t have to write System and a dot before types in the System namespace anymore.
            • I can go through the program, and remove System and the dot each place it occurs.
            • If I save my work and run the program, you can see everything works just as it did before: dotnet run

            If you add a using directive, you don’t have to remove the namespace from the front of type names. But you can.

            Now let me show you what happens if I remove the using directive again.

            • I’ll go to the top of the file, and remove the directive.
            • I’ll save my work, and try running the program again: dotnet run
            • And I get a series of errors:
            • With the using System; directive gone, and without the System namespace in front of each type name, C# can no longer find those types.
            • A couple of the errors even give you a hint: «are you missing a using directive?»
            • So let me undo my changes to add the using System; directive back in.
            • And I’ll save and re-run the program.
            • Everything’s back to working!

            Now let me try a using directive with the unit test.

            • This namespace is really long, so I’m just going to copy it from the main code: Microsoft.VisualStudio.TestTools.UnitTesting
            • Then I’ll go to the top of the file.
            • And type using , a space, paste the namespace, and end the statement with a semicolon.
            • Then I’ll go through the rest of the file, and each place the namespace occurs, I can delete it along with the dot that follows it.
            • Let me save my changes.
            • And now I can run the test: dotnet test
            • I was able to remove that ugly repeated namespace from the code, and everything still works!

            Related Discussions

            Have questions about this video? Start a discussion with the community and Treehouse staff.

            Related Discussions

            Have questions about this video? Start a discussion with the community and Treehouse staff.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *