Namespace c# là gì

Having namespaces seems lượt thích no-brainer for most languages. But as far as I can tell, ANSI C doesn"t tư vấn it. Why not? Any plans to lớn include it in a future standard?



C does have namespaces. One for structure tags, & one for other types. Consider the following definition:

struct foo int a;;typedef struct bar int a; foo;The first one has tag foo, và the later is made into lớn type foo with a typedef. Still no name-clashing happens. This is because structure tags & types (built-in types & typedef"ed types) live sầu in separate namespaces.

Bạn đang đọc: Namespace c# là gì

What C doesn"t allow is lớn create new namespace by will. C was standardized before this was deemed important in a language, and adding namespaces would also threaten backwards-compatibility, because it requires name mangling lớn work right. I think this can be attributed due lớn technicalities, not philosophy.

EDIT:JeremyP. fortunately corrected me and mentioned the namespaces I missed. There are namespaces for labels và for struct/union members as well.

Improve sầu this answer
edited Dec 9 "10 at 19:22
answered Dec 9 "10 at 8:50
Mads ElvheimMads Elvheim
JeremyP : Many thanks for the correction. I only wrote this off memory, I didn't kiểm tra the standard :-) –Mads Elvhelặng Dec 9 "10 at 19:22
| Show 3 more comments
For completeness there are several ways to lớn achieve sầu the "benefits" you might get from namespaces, in C.

Xem thêm: Giải Thích P/S Là Gì ? Ý Nghĩa Của Từ Ps Là Gì Trong Facebook

One of my favorite methods is using a structure lớn house a bunch of method pointers which are the interface khổng lồ your library/etc..

You then use an extern instance of this structure which you initialize inside your library pointing to lớn all your functions. This allows you lớn keep your names simple in your library without stepping on the clients namespace (other than the extern variable at global scope, 1 variable vs possibly hundreds of methods..)

There is some additional maintenance involved but I feel that it is minimal.

Here is an example:

/* interface.h */struct library const int some_value; void (*method1)(void); void (*method2)(int); /* ... */;extern const struct library Library;/* interface.h *//* interface.c */#include "interface.h"void method1(void) ...void method2(int arg) ...const struct library Library = .method1 = method1, .method2 = method2, .some_value = 36;/* end interface.c *//* client code */#include "interface.h"int main(void) Library.method1(); Library.method2(5); printf("%d ", Library.some_value); return 0;/* kết thúc */The use of . syntax creates a strong association over the classic Library_function() Library_some_value method. There are some limitations however, for one you can"t use macros as functions.

Bài viết liên quan