解决方案参考和名称空间问题 [英] solution reference and namespace issue

查看:86
本文介绍了解决方案参考和名称空间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c#解决方案中,当我们添加不同的项目时.我有两个问题:
1)为什么我们要引用另一个项目?我们不能只使用继承吗?
2)在使用Visual Studio IDE提供参考之后,为什么我们必须将项目添加到名称空间系统中?例如:使用blog.entity;我认为很多智能IDE都可以做到这一点.

在此先感谢您为我们提供的启发和启发.

in a c# solution when we add different projects.I have two questions :
1) why we give reference to the other project? can''t we just use inheritance???
2) After we give the reference by using the visual studio IDE why we have to add the project to the namespace system??? for example: using blog.entity; i thought that much intelligent IDE would do that.

thanks in advance for your replies that informs and enlighten.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Blog.Entity;
using System.Data.SqlClient;
using System.Data;

推荐答案

因为默认情况下不会继承任何东西.
添加引用会告诉VS,您要引用DLL中的类-通过实例化它们:
Becaue you aren''t ingheriting anything by default.
Adding a reference tells VS that you want to refer to classes within the DLL - either by instantiation them:
MyDLLClass instance = new MyDLLClass();

或使用静态方法:

Or by using the static methods:

string myString = MyDLLClass.StaticMethod();

此外,您可以将这些类用作自己的类的基础通过继承:

In addition, you can use these classes as a basis for your ouwn classes via inheritance:

public partial class MyClass : MyDLLClass
   {
   ...

但是仅仅因为您可以在DLL文件中引用一个类,并不意味着您想要在每个文件中使用:using名称空间statemkent只允许VS接受快捷方式:不必类型

But just because you can reference a class within a DLL file, doesn''t mean that you want to in every file: the using namespace statemkent just lets VS accept a shortcut: instead of having to type

string s = MyDLLNamepace.MyDLLCLass.StaticMethod();

每次,您都可以键入较短的版本:

each time, you can just type the shorter version:

string s = MyDLLCLass.StaticMethod();

但是您可能不希望在每个文件中都这样-如果您拥有MyDLLClass在两个DLL文件中,在不同的命名空间下?如果它在添加引用时自动在每个文件中添加一个using,则任何现有用途都将在尝试确定要编写哪个版本时出现问题.

But you might not want that in every file - what if you have MyDLLClass in two DLL files, under different namespaces? If it added a using to every file automatically when you added a reference, then any existing uses would have a problem trying to work out which version you meant to write.


这篇关于解决方案参考和名称空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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