namespace NameSpace
{
class Class
{
int F(int X)
{
return X ^ 2;
}
}
}
using System;
using System.Collections.Generic;
.
.
.(etc.)
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
NameSpace.Class obj = new NameSpace.Class();
int X = 1;
int Y;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
Y = obj.F(X);
}
}
}
Normally you would import the namespace, just as you have done for System and System.Collections.Generic, and then use the type name unqualified.
using NameSpace;
Class obj = new Class();
(without "using"):
NameSpace.Class obj = new NameSpace.Class();
Yeah, that's what I said.Yes, but when we insert NameSpace, we can access it by typing:
Without "NameSpace."C#:using NameSpace; Class obj = new Class();
Absolutely but, while you don't ever have to import any namespace, it's usual to import a namespace if you would use it in code more than once. It just makes your code easier to read. The only time you genuinely must qualify a type is when there is a name clash and you need to qualify which of more than one type with the same name you mean.And ... Also without typing "using NameSpace" we should type "NameSpace." To access...
C#:(without "using"): NameSpace.Class obj = new NameSpace.Class();