The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C#


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 06-12-2007, 01:33 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon9 What is the difference between namespace and class?

When I learned C# programming, the only difference between C# and Java is namespace. What is that anyway? Is that the same thing as if you declare a package in Java? What does namespace do?

I realized that it's sort of keeping up all classes together? But, what's the point? Thanx

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 07-07-2007, 06:43 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
namespaces are used to prevent clashing between class and function names. In C++ or C#, when you declare a class or function or variable belonging to a specific namespace, you cannot use that class, function or variable outside of that namespace. For example, in C++, namespaces are referenced in a couple of different ways:
Code:
using namespace std; // you are now able to use things from the std namespace without prefixing them
Code:
using std::cout;
using std::cin;
using std::endl;
// you can now use cout, cin and endl without prefixing them
// however, other members of the std namespace still need the prefixes
Code:
// you must prefix cout, cin and all other members of the std namespace
What did I mean by prefix? This is what I mean by prefix:
Code:
std::cout << "Hello world!" << std::endl;
Also, packages in Java work similarly to namespaces in C# and C++. For example, the "java.lang" package is imported by default, just as you often need to include "using System;" at the top of a C# program to prevent you from needing to use "System.Console.WriteLine" and "System.Convert.ToInt32" and all of that. In C#, you use the using directive to use a namespace without the prefix. In Java, you use the import keyword to import a package to serve a similar (and convenient) purpose. In C++, there are a few ways to use the using keyword and you also need to include the appropriate header files. C++ is basically double the work, but it also has a purpose: prevention of code collision as well as making sure something exists before even trying to compile.


To answer your questions:

C# = namespace, Java = package (basically the same purpose)

a namespace (or package) is used to prevent complications with another person's work because you could name a class or a function with the same name, but the namespace will be different

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-07-2007)
  #3 (permalink)  
Old 07-07-2007, 09:32 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Thank you so much, this is the answer that I'm waiting for a long time already! Really cleared up my confusions about C# namespaces and basically C languages all use namespace right?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 07-07-2007, 09:33 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Quote:
Originally Posted by HelloWorld View Post
Thank you so much, this is the answer that I'm waiting for a long time already! Really cleared up my confusions about C# namespaces and basically C languages all use namespace right?
C# and C++ use namespaces. C does not. I'm not sure about Objective-C or Objective-C++, but C# and C++ definitely do and plain old vanilla C does not.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 07-07-2007, 09:42 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Hey btw, when you use namespace in C# or C++ do they also create the folder automatically for the classes like it is in Java?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #6 (permalink)  
Old 07-07-2007, 09:48 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
No folders are created for namespaces in C++. The namespace is basically locked away on its own in a file (or maybe even multiple files; I can't remember because I've only played with them).

I'm not sure about C#, but the reason why Java creates folders for packages is because that way you can just wrap up the package in a JAR file for easier distribution. Instead of distributing a bunch of classes and hoping someone reads the readme about how to use it, they just need to launch the JAR file.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-08-2007)
  #7 (permalink)  
Old 07-08-2007, 08:30 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
The namespace is basically locked away on its own in a file (or maybe even multiple files; I can't remember because I've only played with them).
I don't really understand of what you mean by locked away on its own in a file it's creating its own file? why...?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #8 (permalink)  
Old 07-08-2007, 02:52 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
You, the programmer, create a file and declare the namespace.

A namespace is often locked away in a header file with various function prototypes and class declarations. The implementation is often in the C++ file that acts as the object code. For example:
Code:
// namespace_example.h
#ifndef _NAMESPACE_EXAMPLE_H_
  #define _NAMESPACE_EXAMPLE_H_

#include <iostream>
namespace example_foo {
    template <typename T>
    void print (const T& t) { std::cout << t << std::endl; }

    void print ( void );
}

#endif
Code:
// namespace_example.cpp
#include "namespace_example.h"

void example_foo::print () { print ("Hello World!"); }
Code:
// tester.cpp
#include "ns_example.h"
#include <string>
using namespace example_foo;
using namespace std;

int main () {
    float x = 2.4;
    string str_bam = "Bam!";
    print();
    print("Hello user!");
    print(x);
    print((int)x);
    print(str_bam);

    return 0;
}
Because the template was in the header, that was required to be implemented there (template implementation cannot be separated from declaration like functions, classes, etc. can be). The special case of no arguments passed to the print() function is implemented in namespace_example.cpp. Of course, I could have implemented it in the header file (namespace_example.h), but I wanted to illustrate how things work.

The prototype for the function print() with no arguments is in the header file while the implementation is in the C++ file "namespace_example.cpp". Also note that in the C++ file, I used "void example_foo::print ()" rather than just "void print ()". This is because I haven't used a using directive to say that print() belongs to the example_foo namespace. However, I didn't need to use it when I called print("Hello World!"); in the function body. This is because I was already allowed to access the namespace. Any member of a namespace has access to any other member of the same namespace. Also note that that C++ file had no int main() function. That's because we don't want to run it. We just need the code inside.

In the tester program, I used two using directives to allow me to use std::string without using "std::" and "example_foo::print" without using "example_foo::". Why didn't I do that in the other files? Basically, the whole idea behind namespaces would be ruined. If you create your own class called "string" and you have "using namespace std;" at the top with <string> included, the compiler will wonder which one you meant.

Anyway, this post is long enough. Just know that namespaces are declared in a file. You can add to that namespace across multiple files if you need to however. For example, "namespace std {" can wrap the entire <istream> header (other than certain C preprocessor directives like #include) as well as it can wrap around the entire <string> header.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-08-2007)
  #9 (permalink)  
Old 07-08-2007, 10:14 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
But the foundation is still namespace is just to organize things classes in C++ together right? Is there any specific purpose of namespace, can we inherits namespaces? or we still have to inherit classes only..?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #10 (permalink)  
Old 07-08-2007, 10:26 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Other than a wrapper to protect code collision and generally wrap code into one single unit, there is no other purpose for a namespace. You can't inherit a namespace to my knowledge, only classes.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-08-2007)
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:19 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50