how to specifically new object in global namespace?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Please check the code below. If I new a object in Main() as "genClass<xxx>",
it will create the class namespace1.genClass<>. So how to create the
genClass<> in the global in Main()?

using System;
class genClass<T>
{
private T _t;
public T T1
{
get { return _t; }
set { _t = value; }
}
public void echo()
{
Console.WriteLine("class genClass<T>");
}
}

namespace namespace1
{
class genClass<T>
{
private T _t;
public T T1
{
get { return _t; }
set { _t = value; }
}
public void echo()
{
Console.WriteLine("generic2.genClass<T>");
}
}

class Program
{
static void Main(string[] args)
{
genClass<int> g = new genClass<int>();
g.T1 = 111;
g.echo();
}
}
}
 
Why would you want to place a class outside of a namespace? It is
strongly recommended that you always use a namespace for any class as
this organizes your classes and makes them easier to document and use
in references.

Best Regards
Johann Blake
 
noname said:
Please check the code below. If I new a object in Main() as "genClass<xxx>",
it will create the class namespace1.genClass<>. So how to create the
genClass<> in the global in Main()?

Change Main to:

static void Main(string[] args)
{
global::genClass<int> g = new global::genClass<int>();
g.T1 = 111;
g.echo();
}

Or, my preferred suggestions:

1) Don't create types without namespaces
2) Try very hard to avoid giving two types the same name
 
Got it. Thanks a lot.

Jon Skeet said:
noname said:
Please check the code below. If I new a object in Main() as "genClass<xxx>",
it will create the class namespace1.genClass<>. So how to create the
genClass<> in the global in Main()?

Change Main to:

static void Main(string[] args)
{
global::genClass<int> g = new global::genClass<int>();
g.T1 = 111;
g.echo();
}

Or, my preferred suggestions:

1) Don't create types without namespaces
2) Try very hard to avoid giving two types the same name
 
Back
Top