Home > Net >  What is difference between using a colon (:) when creating a class to use inheritance and the '
What is difference between using a colon (:) when creating a class to use inheritance and the '

Time:01-12

using System.Web.UI;

namespace MyTestCode
{
    public partial class login: System.Web.UI.Page
    {

Aren't the above just identical ways to include the methods in the System.Web.UI.Page class? But why is one preferred over the other?

I guess there is something I don't understand about inheritance.

CodePudding user response:

using System.Web.UI

Doesn't mean your class is now inheriting anything, it's just a way to import namespaces to be used in your class. You can think of namespaces as libraries, each library has useful classes the can help you create things based on your needs.

For example if i want to do some math operations, there is no need for me to create most of those operations from scrtach, all i have to do is importing the System namespase like this:

using System;

Now since i have the System name space imported, i can use the Math class that lives inside the System namespace:

var floor = Math.Floor(Decimal);

You still can achive this without using the namespace, but you have to do it like this:

var floor = System.Math.Floor(Decimal);

Same goes for your example above, you can do it like this:

    namespace MyTestCode
{
    public partial class login: System.Web.UI.Page
    {

Or you can do this

using System.Web.UI;
namespace MyTestCode
{
    public partial class login: Page
    {

CodePudding user response:

When you use the using keyword, you will have access to all public classes contained in System.Web.UI namespace, this is useful when you need to use a lot of definitions of that namespace, if you only need to use once, you can use directly use System.Web.UI.<class_name>.

  • Related