I've been trying to learn Unity and pretty soon in the tutorials I've found this:  private Rigidbody rb;  void Start () {     rb = GetComponent&lt;Rigidbody&gt;(); }   I'm not knowledgeable with C# syntax so what I really would like to understand is this function&lt;object&gt;() syntax.  Thx
23748
I thought I'd offer this softball to whomever would like to hit it out of the park. What are generics, what are the advantages of generics, why, where, how should I use them? Please keep it fairly basic. Thanks.
89868
In c#, is there any difference in the excecution speed for the order in which you state the condition?  if (null != variable) ... if (variable != null) ...   Since recently, I saw the first one quite often, and it caught my attention since I was used to the second one.  If there is no difference, what is the advantage of the first one?
49724
Take the following two lines of code:  for (int i = 0; i &lt; some_vector.size(); i++) {     //do stuff }   And this:  for (some_iterator = some_vector.begin(); some_iterator != some_vector.end();     some_iterator++) {     //do stuff }   I'm told that the second way is preferred. Why exactly is this?
7323
Is there an official C# guideline for the order of items in terms of class structure?  Does it go:   Public Fields Private Fields Properties Constructors Methods ?   I'm curious if there is a hard and fast rule about the order of items? I'm kind of all over the place. I want to stick with a particular standard so I can do it everywhere.  The real problem is my more complex properties end up looking a lot like methods and they feel out of place at the top before the constructor.  Any tips/suggestions?
449
I keep getting the following error when i try to drag and drop a script to my gameobject:    I have made sure that both my Script name and the class name is : Soldier as you can see:  public class Soldier : MonoBehaviour {     public int Rank; }   So my question is what have i done wrong?
41268
I have seen this syntax in MSDN: , but I don't know what it does.  Does anyone know?
62141
Here is my player code.  Rigidbody rb;  Vector3 currMovement; public float jumpSpeed = 10; public float moveSpeed = 10; public float rotSpeed = 180;  float distToGround;  public float posSmoothTime = 0.5f; float currPos; float targetPos; float posVel;  public float rotSmoothTime = 0.5f; float currRot; float targetRot; float rotVel;  void Start() {     rb = GetComponent&lt;Rigidbody&gt;();     distToGround = GetComponent&lt;Collider&gt;().bounds.extents.y; }  bool isGrounded() { return Physics.Raycast(transform.position, Vector3.down, distToGround + 0.1f); }  void Update() {     Move(); }  void Move() {     // Rotation smoothing.     targetRot = Input.GetAxisRaw("Horizontal") * rotSpeed * Time.smoothDeltaTime;      if (targetRot &gt; 360)         targetRot -= 360;     if (targetRot &lt; 0)         targetRot += 360;     currRot = Mathf.SmoothDampAngle(currRot, targetRot, ref rotVel, rotSmoothTime * Time.smoothDeltaTime);      transform.eulerAngles += new Vector3(0, currRot, 0);      // Movement smoothing.     targetPos = Input.GetAxisRaw("Vertical") * moveSpeed;      currPos = Mathf.SmoothDamp(currPos, targetPos, ref posVel, posSmoothTime * Time.smoothDeltaTime);     currMovement = new Vector3(0, 0, currPos);     currMovement = transform.rotation * currMovement;      if (isGrounded())     {         if (Input.GetButtonDown("Jump"))             rb.velocity += Vector3.up * jumpSpeed;     }      rb.position += currMovement * Time.smoothDeltaTime; }   I have a Rigidbody attached to my player. I think the problem is with my camera script. Here is my camera script.  public Transform player; Quaternion targetLook; Vector3 targetMove; public float smoothLook = 0.5f; public float smoothMove = 0.5f; public float distFromPlayer = 5, heightFromPlayer = 3; Vector3 moveVel;  void LateUpdate() {     CameraMove(); }  void CameraMove() {     targetMove = player.position + (player.rotation * new Vector3(0, heightFromPlayer, -distFromPlayer));     transform.position = Vector3.SmoothDamp(transform.position, targetMove, ref moveVel, smoothMove);      targetLook = Quaternion.LookRotation(player.position - transform.position);     transform.rotation = Quaternion.Slerp(transform.rotation, targetLook, smoothLook); }   }  The player is not an parent of my camera. When I parent the player to my camera the shake stops. But I want a custom smooth camera movement with my custom scirpt, so I can't make the player a parent of the camera.
11253
When you have code like the following:   static T GenericConstruct&lt;T&gt;() where T : new() {     return new T(); }   The C# compiler insists on emitting a call to Activator.CreateInstance, which is considerably slower than a native constructor.   I have the following workaround:  public static class ParameterlessConstructor&lt;T&gt;     where T : new() {     public static T Create()     {         return _func();     }      private static Func&lt;T&gt; CreateFunc()     {         return Expression.Lambda&lt;Func&lt;T&gt;&gt;( Expression.New( typeof( T ) ) ).Compile();     }      private static Func&lt;T&gt; _func = CreateFunc(); }  // Example: // Foo foo = ParameterlessConstructor&lt;Foo&gt;.Create();   But it doesn't make sense to me why this workaround should be necessary.
110522
Possible Duplicate:          I've been seeing that expression for over 10 years now. I've been trying to think what it's good for. Since I see it mostly in #defines, I assume it's good for inner scope variable declaration and for using breaks (instead of gotos.)  Is it good for anything else? Do you use it?
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.
76186
Once it is compiled, is there a difference between:  delegate { x = 0; }   and  () =&gt; { x = 0 }   ?
38358
I've been learning C#, and I'm trying to understand lambdas.  In this sample below, it prints out 10 ten times.  class Program {     delegate void Action();     static void Main(string[] args)     {         List&lt;Action&gt; actions = new List&lt;Action&gt;();          for (int i = 0; i &lt; 10; ++i )             actions.Add(()=&gt;Console.WriteLine(i));          foreach (Action a in actions)             a();     } }   Obviously, the generated class behind the lambda is storing a reference or pointer to the int i variable, and is assigning a new value to the same reference every time the loop iterates.  Is there a way to force the lamda to grab a copy instead, like the C++0x syntax  [&amp;](){ ... } // Capture by reference   vs.  [=](){ ... } // Capture copies
23755
Generally, when using the conditional operator, here's the syntax:  int x = 6; int y = x == 6 ? 5 : 9;   Nothing fancy, pretty straight forward.  Now, let's try to use this when assigning a Lambda to a Func type. Let me explain:  Func&lt;Order, bool&gt; predicate = id == null     ? p =&gt; p.EmployeeID == null     : p =&gt; p.EmployeeID == id;   That's the same syntax, and should work? Right? For some reason that doesn't. The compiler gives this nice cryptic message:     Error  1   Type of conditional expression cannot be determined because there is no implicit conversion between 'lambda expression' and 'lambda expression'   I then went ahead and changed the syntax and this way it did work:  Func&lt;Order, bool&gt; predicate = id == null     ? predicate = p =&gt; p.EmployeeID == null     : predicate = p =&gt; p.EmployeeID == id;   I'm just curious as to why it doesn't work the first way?  (Side note: I ended up not needing this code, as I found out that when comparing an int value against null, you just use object.Equals)
22388
How do you give a C# auto-property an initial value?  I either use the constructor, or revert to the old syntax.    Using the Constructor:  class Person  {     public Person()     {         Name = "Initial Name";     }     public string Name { get; set; } }   Using normal property syntax  (with an initial value)  private string name = "Initial Name"; public string Name  {     get      {         return name;     }     set     {         name = value;     } }   Is there a better way?
45838
If I have the following code:  MyClass pClass = new MyClass(); pClass.MyEvent += MyFunction; pClass = null;   Will pClass be garbage collected? Or will it hang around still firing its events whenever they occur? Will I need to do the following in order to allow garbage collection?  MyClass pClass = new MyClass(); pClass.MyEvent += MyFunction; pClass.MyEvent -= MyFunction; pClass = null;
66959
I've built a few apex controllers, and like most people have copied some code from here and there. In the controllers, there's typically an expression that looks like the following:  public *var type* someName {get; set;}   Initially I thought I needed to supply a function to the get part if the VF page was grabbing data, something like so:  get = getData(); set;  public function string getData(){     return 'data!'; }   And the opposite for set (if users were inputting data). But now I see my controllers work perfectly well just having the top expression as {get;set;}.  I've looked through the salesforce docs and I've read all the parts regarding creating custom controllers - I see the get; set; being referenced, but nowhere explaining what this part does and how I can use it.  Have you come across any docs on what this part does and how I can use it?  Thank you all!  Zac
26363
I've seen the word static used in different places in C code; is this like a static function/class in C# (where the implementation is shared across objects)?