- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Namespaces
In this part of the C# tutorial, we will describe namespaces.
Namespaces are used to organize code at the highest logical level. They classify and present programming elements that are exposed to other programs and applications. Within a namespace, we can declare another namespace, a class, an interface, a structure, an enumeration or a delegate. We cannot define items such as properties, variables and events. These items must be declared within containers such as structures or classes. Namespaces prevent ambiguity and simplify references when using large groups of objects such as class libraries.
Namespaces organize objects in an assembly. An assembly is a reusable, versionable and self-describing building block of a CLR application. Assemblies can contain multiple namespaces. Namespaces can contain other namespaces. An assembly provides a fundamental unit of physical code grouping. A namespace provides a fundamental unit of logical code grouping.
public class CSharpApp { static void Main() { System.Console.WriteLine("Simple namespace example"); } }
The built-in libraries are organized within namespaces. Take the Console class. It is available within the System
namespace. To call the static WriteLine()
method of the Console
class, we use its fully qualified name. Fully qualified names are object references that are prefixed with the name of the namespace where the object is defined.
In the following code, we have two files that share the same namespace.
namespace2.cs
using System; namespace ZetCode { public class Example { public int x = 0; public void Raise() { x += 100; Console.WriteLine(x); } } }
We have a ZetCode
namespace. In the namespace, we have an Example
class.
namespace ZetCode { ... }
We declare a namespace called ZetCode
. The code goes inside the curly brackets of the ZetCode
namespace.
namespace1.cs
namespace ZetCode { public class CSharpApp { static void Main() { Example ex = new Example(); ex.Raise(); ex.Raise(); } } }
In the second file, we work with the Example
class from the previous file. We invoke its Raise()
method.
namespace ZetCode
We work in the same namespace.
Example ex = new Example(); ex.Raise(); ex.Raise();
We create an instance of the Example
class. We call its Raise()
method twice. Because we work with objects of the same namespace, we do not need to specify its name explicitly.
$ dmcs namespace1.cs namespace2.cs $ ./namespace1.exe 100 200
This is the example output.
The following code example has two distinct namespaces. We use the using
keyword to import elements from a different namespace.
distinctnamespace2.cs
namespace MyMath { public class Basic { public static double PI = 3.141592653589; public static double GetPi() { return PI; } } }
We have a skeleton of a Math
class in a MyMath
namespace. In the Basic
class, we define a PI
constant and a GetPi()
method.
distinctnamespace1.cs
using MyMath; using System; namespace ZetCode { public class CSharpApp { static void Main() { Console.WriteLine(Basic.PI); Console.WriteLine(Basic.GetPi()); } } }
In this file, we use the elements from the MyMath namespace.
using MyMath;
We import the elements from the MyMath
namespace into our namespace.
Console.WriteLine(Basic.PI) Console.WriteLine(Basic.GetPI())
Now we can use those elements. In our case it is the Basic
class.
$ dmcs distinctnamespace1.cs distinctnamespace2.cs $ ./distinctnamespace1.exe 3.141592653589 3.141592653589
We compile the two files and run the program.
Root namespace
The root namespace is the mainspace of the .NET Framework libraries. It may happen that someone creates a type or a namespace that will conflict with ones from the .NET Framework. In such cases, we can refer to the root namespace with the global::
prefix.
namespace ZetCode { class System { public override string ToString() { return "This is System class"; } } public class RootNamespace { static void Main() { System s = new System(); global::System.Console.WriteLine(s); } } }
In our ZetCode
namespace, we create a System
class that will clash with the one from the .NET Framework.
System s = new System();
Here we refer to the System
class from the ZetCode
namespace.
global::System.Console.WriteLine(s);
With the global::
prefix, we point to the System
class of the root namespace.
The root namespace is also the default namespace for C# programs. In most programs in this tutorial, we did not specify any namespace. Such programs are added to the unnamed default namespace.
using System; struct Book { public override string ToString() { return "Book struct in a default namespace"; } } namespace MainProgram { struct Book { public override string ToString() { return "Book struct in a MainProgram namespace"; } } public class DefaultNamespace { static void Main() { Book book1; global::Book book2; Console.WriteLine(book1); Console.WriteLine(book2); } } }
We have two Book
structures; one is defined in the MainProgram
namespace, the other is defined outside this namespace.
struct Book { public override string ToString() { return "Book struct in a default namespace"; } }
This Book
structure is defined outside of the custom named MainProgram
namespace. It belongs to the default namespace.
Book book1;
We refer to the structure defined inside the MainProgram
namespace.
global::Book book2;
With the global::
prefix we point to the structure defined outside the MainProgram
namespace.
$ ./default.exe Book struct in a MainProgram namespace Book struct in a default namespace
This is the output of the default.exe
program.
Aliases
The using
keyword can be used to create an alias for a namespace. With nested namespaces, the fully qualified names might get long. We can shorten them by creating aliases.
namespace ZetCode { namespace Items { class Book { public override string ToString() { return "This is a book"; } } } } namespace MainProgram { using ZIB = ZetCode.Items.Book; public class Aliases { static void Main() { ZetCode.Items.Book book = new ZetCode.Items.Book(); ZIB book2 = new ZIB(); System.Console.WriteLine(book); System.Console.WriteLine(book2); } } }
In the example, we create an alias for a Book
class that is enclosed by two namespaces.
namespace ZetCode { namespace Items { class Book { ... } } }
It is possible to nest a namespace into another namespace. The fully qualified name of the Book
class is ZetCode.Items.Book
.
using ZIB = ZetCode.Items.Book;
The using
keyword createas a ZIB
alias for the fully qualified name ZetCode.Items.Book
.
ZetCode.Items.Book book = new ZetCode.Items.Book(); ZIB book2 = new ZIB();
We use both names to create a book instance.
This part of the C# tutorial was dedicated to namespaces.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论