Home  >  Article  >  Backend Development  >  Packages in C#

Packages in C#

PHPz
PHPzforward
2023-09-10 21:17:021046browse

C# 中的包

As an alternative to packages in Java, the C# language has namespaces.

Packages in Java

Packages are used in Java to prevent naming conflicts, control access, and make it easier to search/locate and use classes, interfaces, enumerations, annotations, etc.

Namespaces in C

#Namespaces are designed to provide a way to keep one set of names separate from another. A class name declared in one namespace does not conflict with the same class name declared in another namespace.

The namespace definition begins with the keyword namespace, followed by the namespace name. Below shows how to use namespaces in C# -

Example

using System;

namespace first_space {
   class namespace_cl {
      public void func() {
         Console.WriteLine("Inside first_space");
      }
   }
}

namespace second_space {
   class namespace_cl {
      public void func() {
         Console.WriteLine("Inside second_space");
      }
   }
}

class TestClass {
   static void Main(string[] args) {
      first_space.namespace_cl fc = new first_space.namespace_cl();
      second_space.namespace_cl sc = new second_space.namespace_cl();

      fc.func();
      sc.func();
      Console.ReadKey();
   }
}

The above is the detailed content of Packages in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete