I recently watched a tutorial that declared an instance of a new object using the following method:  class exampleClass : IEnumerable {     //Class stuff... }  class Main {     public void Method1     {         IEnumerable Obj1 = new exampleClass();     } }   I couldn't work out what the purpose or benefit of it was. I've always declared the type I will be instantiating so this way of doing it seemed alien.  Please could someone explain why you would do this and what the benefits are?  (The example above is purely to highlight the declaration of the new objects' type and then calling to instantiate a class of a different type.)
685
I have seen this mentioned a few times and I am not clear on what it means. When and why would you do this?   I know what interfaces do, but the fact I am not clear on this makes me think I am missing out on using them correctly.   Is it just so if you were to do:  IInterface classRef = new ObjectWhatever()   You could use any class that implements IInterface? When would you need to do that? The only thing I can think of is if you have a method and you are unsure of what object will be passed except for it implementing IInterface. I cannot think how often you would need to do that.   Also, how could you write a method that takes in an object that implements an interface? Is that possible?
20286
I have quickly read over the  documentation.  This kind of example has helped me to understand better, though:   delegate int del(int i); del myDelegate = x =&gt; x * x; int j = myDelegate(5); //j = 25   Still, I don't understand why it's such an innovation. It's just a method that dies when the "method variable" ends, right? Why should I use this instead of a real method?
27319
I want to use a collection initializer for the next bit of code:  public Dictionary&lt;int, string&gt; GetNames() {     Dictionary&lt;int, string&gt; names = new Dictionary&lt;int, string&gt;();     names.Add(1, "Adam");     names.Add(2, "Bart");     names.Add(3, "Charlie");     return names; }   So typically it should be something like:  return new Dictionary&lt;int, string&gt; {     1, "Adam",    2, "Bart"    ...   But what is the correct syntax for this?
8134
The  says that public class SomeObject {   public void SomeOperation()   {     lock(this)     {       //Access instance variables     }   } }  is &quot;a problem if the instance can be accessed publicly&quot;. I'm wondering why? Is it because the lock will be held longer than necessary? Or is there some more insidious reason?
109568
Is it possible to reflectively instantiate a generic type in Java? Using the technique described  I get an error because class tokens cannot be generic. Take the example below. I want to instantiate some subclass of Creator that implements Creator. The actual class name is passed in as a command line argument. The idea is to be able to specify an implementation of Creator at runtime. Is there another way to accomplish what I'm trying to do here?  public interface Creator&lt;T&gt; {     T create(); } public class StringCreator implements Creator&lt;String&gt; {     public String create() { return new String(); } } public class FancyStringCreator implements Creator&lt;String&gt; {     public String create() { return new StringBuffer().toString(); } } public static void main(String[] args) throws Exception {     Class&lt;?&gt; someClass = Class.forName(args[0]);     /*ERROR*/Class&lt;? extends Creator&lt;String&gt;&gt; creatorClass = someClass.asSubclass(Creator.class);     Constructor&lt;? extends Creator&lt;String&gt;&gt; creatorCtor = creatorClass.getConstructor((Class&lt;?&gt;[]) null);     Creator&lt;String&gt; creator = creatorCtor.newInstance((Object[]) null); }   Edit: I like Marcus' approach as being the most simple and pragmatic without circumventing the whole generics thing. I can use it in my situation because I can specify that the class passed must be a subclass of StringCreator. But as Ericson pointed out the generic information is still there at the type level, just not at the runtime level so it is still possible to reflectively examine whether a given class implements the correct generic type.
77567
I've read in the  and the  the mention of inner classes. What is their purpose?  I see in the Apex Workbook that they used private variables and setter/getter methods in the inner class, but why didn't they just do that in the outer class with a constructer, getter, and setter?
62585
Given the following example, why do I have to explicitly use the statement b-&gt;A::DoSomething() rather than just b-&gt;DoSomething()?  Shouldn't the compiler's overload resolution figure out which method I'm talking about?  I'm using Microsoft VS 2005. (Note: using virtual doesn't help in this case.)  class A {   public:     int DoSomething() {return 0;}; };  class B : public A {   public:     int DoSomething(int x) {return 1;}; };  int main() {   B* b = new B();   b-&gt;A::DoSomething();    //Why this?   //b-&gt;DoSomething();    //Why not this? (Gives compiler error.)   delete b;   return 0; }
113807
Why does the following code not work?  I want to instantiate prefabs in a list and then use  it as a GameObject like that :  public GameObject prefab;  void Start() {   ArrayList players = new ArrayList();    for(int i=0; i&lt;5; i++)   {         players.Add( (GameObject)Instantiate(prefab));   }    players[2].transform.position = new Vector3(1, 1, 1);  }   there is an error message
67180
I remember reading in some Java book about any operator other than 'instanceof' for comparing the type hierarchy between two objects.  instanceof is the most used and common. I am not able to recall clearly whether there is indeed another way of doing that or not.
46209
Using instance methods as callbacks for event handlers changes the scope of this from "My instance" to "Whatever just called the callback". So my code looks like this  function MyObject() {   this.doSomething = function() {     ...   }    var self = this   $('#foobar').bind('click', function(){     self.doSomethng()     // this.doSomething() would not work here   }) }   It works, but is that the best way to do it? It looks strange to me.
56347
The  keyword does away with the need for an explicit type declaration and I have read with interest the  of when it might be appropriate.  I have also read about (but not used)  which seems to take things a step further by making it .  With Boo, both the type and the declaration can be implied.  Which leads me to wonder, why did the C# language designers bother to include a var keyword at all?    Update: Yes, var supports Anonymous types, but anonymous types by themselves do not necessitate the var keyword...  var anon = new { Name = "Terry", Age = 34 };   versus  anon = new { Name = "Terry", Age = 34 };
81621
If I write the following class:  public class Example {        int j;       int k;        public Example(int j, int k) {            j = j;            k = k;       }        public static void main(String[] args) {            Example exm = new Example(1,2);            System.out.println(exm.j);            System.out.println(exm.k);       }  }   The program compiles, but when I run the program, the main method will print out two 0s. I know that in order to say that I want to initialize the instance variables in the constructor I have to write:  this.j = j; this.k = k;   But if I don't write it, then which variable is evaluated (or considered) in the constructor (on the left and on the write hand side of the expressions)? Is is the argument or the instance variable? Does it make a difference?  Are there other cases where the use of this is obligatory?
118553
In C++ I can't use a method if I declare it after the calling method.    Does this order matter in other languages like Java or C#?
30163
What is the difference between this:  void MyMethod(IMyInterface value) {     //... }   and this:  void MyMethod&lt;T&gt;(T value) where T : IMyInterface {     //... }
33578
I'm learning the Python programming language and I've came across something I don't fully understand.   In a method like:  def method(self, blah):     def __init__(?):         ....     ....   What does self do?  What is it meant to be?  Is it mandatory?  What does the __init__ method do?  Why is it necessary? (etc.)  I think they might be OOP constructs, but I don't know very much.
89
I'm not that into dynamic programming languages but I've written my fair share of JavaScript code. I never really got my head around this prototype-based programming, does any one know how this works? var obj = new Object(); obj.prototype.test = function() { alert('Hello?'); }; var obj2 = new obj(); obj2.test();  I remember a lot discussion I had with people a while back (I'm not exactly sure what I'm doing) but as I understand it, there's no concept of a class. It's just an object, and instances of those objects are clones of the original, right? But what is the exact purpose of this &quot;.prototype&quot; property in JavaScript? How does it relate to instantiating objects? Update: correct way var obj = new Object(); // not a functional object obj.prototype.test = function() { alert('Hello?'); }; // this is wrong!  function MyObject() {} // a first class functional object MyObject.prototype.test = function() { alert('OK'); } // OK  Also these  really helped a lot.
78162
I collect a few corner cases and  and would always like to hear more. The page only really covers C# language bits and bobs, but I also find core .NET things interesting too. For example, here's one which isn't on the page, but which I find incredible:  string x = new string(new char[0]); string y = new string(new char[0]); Console.WriteLine(object.ReferenceEquals(x, y));   I'd expect that to print False - after all, "new" (with a reference type) always creates a new object, doesn't it? The specs for both C# and the CLI indicate that it should.  Well, not in this particular case. It prints True, and has done on every version of the framework I've tested it with. (I haven't tried it on Mono, admittedly...)  Just to be clear, this is only an example of the kind of thing I'm looking for - I wasn't particularly looking for discussion/explanation of this oddity. (It's not the same as normal string interning; in particular, string interning doesn't normally happen when a constructor is called.) I was really asking for similar odd behaviour.  Any other gems lurking out there?