|
And what about this?
namespace test
{
class Base { }
class Test1 : Base { void Test() { Console.WriteLine("Test performed by Test1"); } }
class Test2 : Base { void Test() { Console.WriteLine("Test performed by Test2"); } }
class Test3 : Base { }
class Box<T>
{
public T content;
}
class Program
{
static void Main(string[] args)
{
Box<Base> bb = new Box<Base>;
Random r = new Random();
int i = r.Next() % 3;
switch (i)
{
case 0:
bb.content = new Test1();
break;
case 1:
bb.content = new Test2();
break;
case 2: bb.content = new Test3();
break;
}
if (i < 2)
bb.content.Test(); // error
else
Console.WriteLine("Can't perform test.");
}
}
}
To those who understand, I extend my hand.
To the doubtful I demand: Take me as I am.
Not under your command, I know where I stand.
I won't change to fit yout plan. Take me as I am.
|
|
|
|
|
I'll just take your word on that one. :P
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
I'm sorry, I got it TOTALLY wrong with my examples. But here's a better comparison between templates and generics:
http://blogs.msdn.com/branbray/archive/2003/11/19/51023.aspx[^]
To those who understand, I extend my hand.
To the doubtful I demand: Take me as I am.
Not under your command, I know where I stand.
I won't change to fit yout plan. Take me as I am.
|
|
|
|
|
Cool! I'll check it out later, now that u've piqued my curiosity.
Thanks
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
Ahhh, so the big difference (in my mind at least) is that templates are compile-time "specialized", while generics are run-time specialized. This means that with templates and C++ you can basically treat the type parameter as whatever you want, and the compiler will tell you if you can't do it.
With generics and C#, there is no such freedom because you must specify all of your constraints up-front (since generics are specializable at runtime and therefore must be able to accept any compatible type, not just the ones you happen to use).
Now that I remember a little, I did like that about C++, and it is one thing I find particularly annoying about C#, so thanks for clarifying that for me.
Logan
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
Also...
leonej_dt wrote: Private NextNode As LinkedList(Of T) ' the implicit pointer... yuck!
How is this any different from:
leonej_dt wrote: CLinkedList<t> *m_plltNext;
I don't understand your "yuck!" could you please explain that to me? Are you referring to the fact the .NET keeps a reference as opposed to a pointer, and thus keeps the object alive?
[Update: Oh I see, the C++ version isn't type-specific, so you can link together instances of different-templated LinkedLists. Hmm that's interesting, but you could still do this in C# without generics, by using a common base type or interface (or even just object).]
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
One problem I do have with generics is that there is no "is a" relationship between GenericClass<SubType> and GenericClass<BaseType>.
For example, List<Button> "IS A" List<Control>, but C# doesn't accept this, to my knowledge.
|
|
|
|
|
No! A List IS NOT a List<control> because a List can't contain Controls. The fact both lists could contain Buttons is just a mere coincidence.
To those who understand, I extend my hand.
To the doubtful I demand: Take me as I am.
Not under your command, I know where I stand.
I won't change to fit yout plan. Take me as I am.
|
|
|
|
|
Hm, I see your point: you wouldn't want to allow a List-Button (i'm skipping the freaking angle-brackets, yeesh enough probs already) to have Controls added to it--because it should guarantee that it contains only Buttons--but it should be interpretable as a List-Control for the purpose of some method or operation that expects a List-Control.
So I guess what I mean is, the "is a" relationship should apply to read-only interpretations (casts if you will) of the original. C# has no ground in this area at all.
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
Snork!
I was wrestling with generics today (C#) and was surprised that I can't cast a List<T> to List<object>!
|
|
|
|
|
As leonej_dt pointed out, the real reason this is the case is C# does not have a built-in way to make an entire object read-only. In order for this to work, the cast would have to result in a List<object> that could be read, but not added to (otherwise we'd be adding objects of the wrong type to the collection!), unless somehow the Add method still required T's and not objects.
So a simple cast (as it currently stands) is not an option.
It's certainly theoretically possible to do, but it's just not something that's been done yet, not that I follow other languages very closely. It would probably require a lot of re-thinking and a lot of existing concepts would have to change.
Logan
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
Oh, yeah, there is that...
|
|
|
|
|
From syntax point, yes.
But you don't program only with syntax. The library is important.
The libraries are very different.
|
|
|
|
|
The libraries are different, but the basic concept behind them is the same: "Oh, poor programmer, he has to deal with low level details, let's make life simpler for him...".
To those who understand, I extend my hand.
To the doubtful I demand: Take me as I am.
Not under your command, I know where I stand.
I won't change to fit yout plan. Take me as I am.
|
|
|
|
|
To that I call productivity... it doesn't mean that the programmer has less skills than the other that codes low level...
|
|
|
|
|
That's called abstraction and it's the key to what makes building complex systems possible!
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
Yes, but they don't let me mess with machine code, or even deal with the OS directly, NOT EVEN WHEN I WANT TO. The exceptions are Delphi, C and C++, but nobody uses Delphi and new programmers tend to dislike C and C++ and prefer either...
a) ... languages that are not compiled to machine code, but some sort of intermediate code that is either JITed or interpreted by a VM... which I hate because they add unnecessary type checkings and that kind of things, or,
b) ... script languages (yuck!). Theeeeeeeeeey aaaaaaaaaaaare slooooooooooooooooow. Peeeeeeeeeeerioooooooooooooood.
To those who understand, I extend my hand.
To the doubtful I demand: Take me as I am.
Not under your command, I know where I stand.
I won't change to fit yout plan. Take me as I am.
|
|
|
|
|
That's not true... .NET Framework and Java let's you deal with OS directly:
.NET Framework -> PInvoke
Java -> JNI
|
|
|
|
|
Yes, but... why.do.i.have.to.use.a.special.feature.to.deal.with.the.os??? do.i.have.to.ask.permission.to.deal.with.the.os.which.is.supposed.to.be.for.us.programmers.to.deal.with???
To those who understand, I extend my hand.
To the doubtful I demand: Take me as I am.
Not under your command, I know where I stand.
I won't change to fit yout plan. Take me as I am.
|
|
|
|
|
The very first reason is because both platforms don't have equivalents to the entire Windows API (as an example), so they granted a way to deal with the OS directly.
Why you have to use a special feature? Because it's a special feature. If you want to code dealing with the OS directly it's easy... just don't use platforms like .NET or Java.
The idea is that you don't need to handle the OS and how it do the things. It simplifies things, but of course has its disavantages that u mentioned before: some lack of performance.
|
|
|
|
|
A big difference between C# and Java is that if you choose to, you can you use pointers in C# (and thus a whole lot of relatively low level stuff). Also, C# has events, and real events make event based programming a lot nicer (and no, those event-interfaces of Java are not events, not even close)
The Java compiler is also weird, it requires classes to be in their own file, which must have the same name as that class. Such limitations are only annoying, if you'd want to separate your classes like that you'd do it anyway, but Java forces you to do it.
Java has no structs. So how do you define your own value types? Throwing everything except primitive types in the heap isn't very efficient. And there is no operator overloading..
Java really is bad for programmers, I even heard my teacher for Imperative Programming say that "you do not have to worry about things like the stack and other low level things like that" - how could anyone program efficiently while ignoring such things? "Other low level things" might include locality of reference and the cost of indirection, at least looking at his example code leads me to think that he doesn't care about important performance aspects such as those.
|
|
|
|
|
I switched from c++ to java and now I'm back at c++ again. Forcing the class and file name to be same is good: should be that way in all languages IMHO.
Structs? never used them even in c++. They are a leftover from c.
Operator overloading, you don't need it and many times it causes confusion.
The real things that I thought were lacking in Java is the fact that you can't return more than one value from a function, multiple inheritance and the garbage collection, which is a double edged sword (i.e. you have problem of unintended object retention)
|
|
|
|
|
ed welch wrote: Structs? never used them even in c++. They are a leftover from c.
<br />
I Love My Struct's.<br />
There Puprose Is Clear.<br />
They Move Your Data<br />
From Here To There.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"How do you find out if you're unwanted if everyone you try to ask tells you to go away?" - Balboos HaGadol
|
|
|
|
|
programmer poetry??
|
|
|
|
|
And a pun, too:
Rhyming poetry -> English with struct(ure).
Seriously:
Coding is poetry. It is art.
At least if done well.
An application that flows naturally; pleasing to the eyes and to the senses.
A subtle harmony.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"How do you find out if you're unwanted if everyone you try to ask tells you to go away?" - Balboos HaGadol
|
|
|
|