d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Or C#
Prev12
Add Reply New Topic New Poll
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 9 2016 11:10pm
Quote (Klexmoo @ Mar 9 2016 07:32am)
I don't see any issue?
C# generics are a part of the language all the way through the CLR, and generics in Java are (afaik) compiled to typecasts and are not "real" generics.
You can use simple type notation in C# (can use string in lower case and Boolean -> bool in C#, not that it matters though)

*edit:
I ran the program in Java and the output is different so I guess I was wrong.

I think it has something to do with how abstract classes work in Java vs C# but I'm not entirely sure, as I don't code in Java.

Each instantiation you do (in your C# example) of BagOfStrings and BagOfBooleans calls the individual constructors.
The reason I think that they aren't the same value when printing to the console, is that you print the value of the integer for each individual instance, even though it's a static variable.

Abstract classes in C# aren't meant to be instantiated, so I think (I'm not 100% on this) that you simply have the static variable as a member of each BagOfStrings and BagOfBooleans, rather than them sharing the same static variable from their parent class, because the abstract class is never instantiated.


I know exactly what is happening. I knew before I posted the question. You must be new. What I did is called "being an asshole." When some random shows up claiming they want to "help" with programming assignments because they "know" a language it screams "I am a first year university student and I want some extra fg so I will do your homework for you." So, I responded.

But just so we are clear, in C# static members are shared across all instances of the same closed constructed type. This means in my example, I have 2 closed constructed types of the generic type Bag<>. This means each variant, Bag<string> and Bag<bool> will have their own copy of the static member Capacity. This is different from Java, which uses Type Erasure. Java ditches the types at runtime, so thereis no way to specify which type it is, so it gets one copy for the generic. I demonstrated this in my example. The reason I asked this question is because it demonstrates a deeper understanding of both languages. If you are going to offer your services as a tutor to those coming to this forum then you should know a little bit more about the language than just how to write a for-loop. It's annoying.

This post was edited by Minkomonster on Mar 9 2016 11:12pm
Member
Posts: 23,838
Joined: Feb 18 2009
Gold: 0.01
Mar 10 2016 02:36am
God Damn...get the aloe vera
Member
Posts: 12,786
Joined: May 17 2013
Gold: 4,010.00
Mar 10 2016 01:15pm
Quote (Minkomonster @ 10 Mar 2016 06:10)
I know exactly what is happening. I knew before I posted the question. You must be new. What I did is called "being an asshole." When some random shows up claiming they want to "help" with programming assignments because they "know" a language it screams "I am a first year university student and I want some extra fg so I will do your homework for you." So, I responded.

But just so we are clear, in C# static members are shared across all instances of the same closed constructed type. This means in my example, I have 2 closed constructed types of the generic type Bag<>. This means each variant, Bag<string> and Bag<bool> will have their own copy of the static member Capacity. This is different from Java, which uses Type Erasure. Java ditches the types at runtime, so thereis no way to specify which type it is, so it gets one copy for the generic. I demonstrated this in my example. The reason I asked this question is because it demonstrates a deeper understanding of both languages. If you are going to offer your services as a tutor to those coming to this forum then you should know a little bit more about the language than just how to write a for-loop. It's annoying.


I haven't coded in Java (ever) but after reading around a bit it became pretty obvious to me that this is an artifact of java not being designed with generics in mind (like C# was before 2.0)

I realize what I said in my reply was wrong, but the edit button was gone when I realized it had nothing to do with the instantiation of the classes, but only with generics, and didn't bother with making a new post.

Thanks for showing the example, it was really interesting to me though.

This post was edited by Klexmoo on Mar 10 2016 01:16pm
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll