c#中新建的类,如果没有给它指定命名空间,那么它的命名空间是什么?另外,类外可以定义变量吗?

[复制链接]
查看11 | 回复4 | 2011-5-31 17:09:02 | 显示全部楼层 |阅读模式
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//在此可以定义变量吗?
delegate void TimeDelegate(string s);//委托类似于类吧,如果在此定义变量的话可以吗?是不是这里只能定义类、委托或者结构呢?还有没有其他呢?

class Mytime //没有指定命名空间,默认是?

{

public static void HelloTime(string s)

{

Console.WriteLine("Hello{0}!The time is {1} now",s,DateTime.Now);

}

public static void GoodbyeTime(string s)

{

Console.WriteLine("Goodbye{0}!The time is {1} now",s,DateTime.Now);

}

publicvoid SayTime(string s)

{

Console.WriteLine("{0}!The time is {1} now",s,DateTime.Now);

}

}
class TestDelegate

{

public static void Main()

{

TimeDelegate a = new TimeDelegate(Mytime.HelloTime);

Console.WriteLine("Invoking delegate a:");

a("A");

TimeDelegate b = new TimeDelegate(Mytime.GoodbyeTime);

Console.WriteLine("Invoking delegate b:");

b("B");

TimeDelegate c = a+b;

Console.WriteLine("Invoking delegate c:");

c("C");

c-=a;

Console.WriteLine("Invoking delegate c:");

c("C");

Mytime t = new Mytime();

TimeDelegate d= new TimeDelegate(t.SayTime);

Console.WriteLine("Invoking delegate d:");

d("D");

}

}

回复

使用道具 举报

千问 | 2011-5-31 17:09:02 | 显示全部楼层
默认命名空间跟你的项目名称一样。在类外面还可以定义枚举,事件等。...
回复

使用道具 举报

千问 | 2011-5-31 17:09:02 | 显示全部楼层
应该是项目名吧,没有指定命名空间,其他类里是可以直接引用的...
回复

使用道具 举报

千问 | 2011-5-31 17:09:02 | 显示全部楼层
delegate void TimeDelegate(string s);委托本身也是一个特殊的类的定义啊, //没有指定命名空间,默认就是最外层的了...
回复

使用道具 举报

千问 | 2011-5-31 17:09:02 | 显示全部楼层
global::Mytime...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行