帮助理解整个类名空间的事情。 [英] Help understanding the whole class name space thing.

查看:65
本文介绍了帮助理解整个类名空间的事情。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,只是改变职业生涯并学习C#。我玩过C和Swift,并且了解基础知识。到目前为止,C#还可以,但最让我感到震惊的是名称空间部分。我只是无法绕过它。有人可以像一年级学生一样向我解释。我观看了许多YouTube视频,我知道它们的定义,但它们的大局是什么?如何在真正完整的程序中实际使用它们?非常感谢任何愿意提供帮助的人。



我的尝试:



使用Youtube和Udemy自己学习。

Hi all, Just changing careers and learning C#. I've played around with C, and Swift and know the basics. C# is ok so far but whats holing me back is the name space section. I just can't wrap my head around it. Can somebody here explain it to me like a 1st grader. I watched many youtube videos and I know the "definition" of them, but what are they in terms of the big picture? How to actually use them in a real complete program? Anyone willing to help is greatly appreciated.

What I have tried:

Been learning on my own using Youtube and Udemy.

推荐答案

这很简单。这是一种通过为代码提供合理的名称来将代码组织成不同块的方法。例如:假设您的项目名为Optima。



您的应用程序对象位于Optima.BusinessLogic命名空间中。您的表单可能在Optima.UI中,您的数据库访问权限分组在Optima.DataAccess命名空间中。



更多,您可以在与Optima分开的项目中拥有常用功能库,例如MyCommonMethods.Utilities(你在其中编写所有你在所有代码中有用的方法 - 例如扩展)或MyCommonMethods.Enums,MyCommonMethods.ApplicationAccess等等。



这使您可以使用Optima.BusinessLogic或Optima.DAL引用整组对象,而不会有与库,第三方库或.NET中现有对象发生冲突的风险。



再举一个例子:如果您有在MyCommonMethods中将字符串转换为XML的方法,名为StringToXML,您可以在Optima.BusinessLogic中使用具有相同参数的同名方法,具有完全不同的功能(但特定于您的Optima项目) 。



如果这有帮助,请接受它作为解决方案。谢谢。
It's very simple. It is a way to organise your code into distinct chunks by giving them sensible names. For example: lets say your project is called Optima.

You have your application objects in Optima.BusinessLogic namespace. Your forms might be in Optima.UI and your database access grouped in Optima.DataAccess namespace.

More, you could have library of common function in a project separate from Optima, for example MyCommonMethods.Utilities (where you write all the methods you find useful in all your code - such as extensions) or MyCommonMethods.Enums, MyCommonMethods.ApplicationAccess or some such.

This enables you to reference whole bunch of objects by using Optima.BusinessLogic or Optima.DAL without risking conflicts with existing objects either in your libraries, third party libraries or .NET.

Again an example: if you have method to convert your string to XML in MyCommonMethods called StringToXML, you could have same-named method with same parameters in Optima.BusinessLogic with totally different functionality (but specific to your Optima project).

If this helps, please accept it as a solution. Thank you.


一年级学生 - 不知道是什么年龄。

我做了一些谷歌搜索:)并假设一个5岁。



将你的命名空间问题作为命名约定类型的东西。

-----

这是一个链接在更技术方面 - 不是为5岁的孩子理解,但它可以帮助你。

命名空间的名称 [ ^ ]



或另一个可能有助于您了解命名空间的问题。

c#相同的类名称不同的命名空间

c# - 如何在不同的命名空间中处理相同的类名? - 堆栈溢出 [ ^ ]

c# - 不同名称空间中相同的类名 - Stack Overflow [ ^ ]



以上是技术性的,不适合5岁。

-----

如果他们了解Windows资源管理器的工作原理,这里有一个11岁的人。



想一个文件和/或文件夹名称等同于ClassName(排序)



只要父文件夹是,您就可以拥有一个同名的文件/文件夹不同。

如果您查看系统驱动器,您应该有两个文件夹名称Program Files和

程序文件(x86)。



在这两个文件夹中有一个名称相同的文件夹,例如 Windows NT。

如果我们要求11岁的人删除Windows NT文件夹:),他们会删除哪一个?



为了更具体,我们可以将文件夹名称放入NameSpace类型表示法。

程序文件。Windows NT

程序文件(x86)。Windows NT



所以如果我们问的话他们要删除程序文件(x86)。Windows NT,他们会知道要删除哪一个。

----- -

好​​的,这是一个5岁的孩子。



让我们说一年级学生在学校,凯文是他们最好的朋友。

但是那里有两个Kevin在同一个班级(让我们假设最好的朋友Kevin也在同一个班级)。



所以你问他们哪个凯文?

你知道男孩姓吗?



他们说凯文科斯特纳是我最好的朋友而不是凯文培根。



你也可以在这些名字上使用NameSpace表示法。

Costner.Kevin



Bacon.Kevin



现在你可以确定哪一个邀请参加茶点约会。

-----



希望能帮到你,我没有得到错误的结束:)
1st grader - not sure what age that is.
I done a bit of googling :) and assume a 5 year old.

Taking your question of namespace as a naming convention type thing.
-----
Here is a link on the more technical side - not for a 5 year old to understand but it may help you.
Names of Namespaces[^]

Or an alternative question which may also help you understand namespace.
"c# same class name different namespace"
c# - How to handle same class name in different namespaces? - Stack Overflow[^]
c# - Identical class names in different namespaces - Stack Overflow[^]

The above are a bit technical so not suited to a 5 year old.
-----
Here is one for a 11 year old if they understand how windows explorer works.

Think of a file and/or folder name as being equivalent to a ClassName (sort of)

You can have a file/folder with the same name as long as the the parent folder is different.
If you look into the system drive you should have two folders name "Program Files" and
"Program Files (x86)".

Within these two folder there are folder(s) with the same name, for example "Windows NT".
If we asked the 11 year old to delete the "Windows NT" folder :), which one would they delete?

To be more specific we could put the folder names into NameSpace type notation.
"Program Files"."Windows NT"
"Program Files (x86)"."Windows NT"

So if we asked them to delete "Program Files (x86)"."Windows NT", they would know which one to delete.
------
Okay here is one for a 5 year old.

Lets say the 1st grader was in school and Kevin was their best friend.
But there where two Kevin's in the same class (lets us assume best friend Kevin is in the same class also).

So you ask them which Kevin?
Do you know the boys surname?

And they say "Kevin Costner is my best friend not Kevin Bacon".

Well you can use NameSpace notation on these names too.
Costner.Kevin
and
Bacon.Kevin

So now you can ascertain which one to invite over for a tea date.
-----

Hope that helps you out and I did't get the wrong end of the stick :)


存在名称空间只是为了避免名称冲突。例如,假设您定义一个对象一个名为 File 的类。这样的名称已经被。 .NET Framework 使用,并且会发生冲突。但是,由于 .NET 文件实际上属于 System.IO namespace,其完全限定名称是 System.IO.File ,并且没有名称冲突。
Name spaces exist just to avoid name clashes. Suppose, for instance, you define an object a class called File. Such a name is already used by the .NET Framework and there would be a collision. However, since the .NET File actually belongs to the System.IO namespace, its fully qualified name is System.IO.File and there is no name clash.


这篇关于帮助理解整个类名空间的事情。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆