In C#, a namespace is a container for a set of related classes. It is used to organize and group related classes and avoid naming conflicts.
For example, consider the following two classes:
public class Point
{
public int X { get; set; }
public int Y { get; set; }
}
public class Circle
{
public Point Center { get; set; }
public double Radius { get; set; }
}These two classes might be part of a namespace called Shapes, which could be defined like this:
namespace Shapes
{
public class Point
{
public int X { get; set; }
public int Y { get; set; }
}
public class Circle
{
public Point Center { get; set; }
public double Radius { get; set; }
}
}To use the classes in the Shapes namespace, you need to include a using directive at the top of your code file:
using Shapes;
// Now you can use the Point and Circle classes without fully qualifying their names
Point p = new Point();
Circle c = new Circle();You can also specify the fully qualified name of the class, which includes the namespace, if you want to disambiguate between two classes with the same name in different namespaces:
Shapes.Point p = new Shapes.Point();You can also nest namespaces within other namespaces to create a hierarchy of related classes. For example:
namespace Geometry
{
namespace Shapes
{
public class Point
{
public int X { get; set; }
public int Y { get; set; }
}
public class Circle
{
public Point Center { get; set; }
public double Radius { get; set; }
}
}
} To use the Point and Circle classes in this case, you need to include a using directive for the nested namespace:
using Geometry.Shapes;
Point p = new Point();
Circle c = new Circle();Or, you can use the fully qualified name:
Geometry.Shapes.Point p = new Geometry.Shapes.Point();
Comments
Post a Comment