Search results
Top results related to what is dictionary in c# terms
Top Answer
Answered Apr 13, 2015 · 36 votes
IDictionary is for key->value maps, ICollection is for sets of similar objects.
ICollection is an interface for collections of similar objects: the controls on a form, the elements in a list, the attributes in an XML tag, and so on. As of .NET 2.0, there's a generic version, so you can refer to a collection of integers as ICollection<int>.
IDictionary is an interface for mapping one type of object or value to another. It works like a real dictionary, or a phone book: you have a "key" in mind like a person’s name, and when you look it up, you get some information that’s identified by that key, like an address or phone number. Each key can only be listed once, although two different keys are still allowed to have the same value. This is also generic in .NET 2.0, so a dictionary whose keys are strings and whose values are integers would be IDictionary<string,int>.
A dictionary is actually a collection of key/value pairs: you can use an IDictionary<int,string> as an ICollection<KeyValuePair<int,string>>, and you can access the keys and values as separate collections with the Keys and Values properties.
Both ICollection and IDictionary are unordered, meaning that although you can retrieve the elements in some order with the CopyTo method or a foreach loop, that order has no special meaning, and it might change for no apparent reason. That’s the main difference between ICollection and IList: a list lets you put items in specific positions, just like an array, and they stay there until you move them.
1/5
Top Answer
Answered Jan 13, 2022 · 27 votes
First, AFAIK, there isn't any list that takes two type parameters - so your List could either be a List<int> or List<string> (or a SortedList<int,string>, but that's a different class).
A Tuple<T1, T2>
holds two values - one int and one string in this case. You can use a Tuple to bind together items (that might be of different types). This may be useful in many situations, like, for instance, you want to return more than one value from a method.
I personally hate the Item1, Item2 property names, so I would probably use a wrapper class around Tuples in my code.
A Dictionary<TKey, TValue>
is a collection of KeyValuePair<TKey, TValue>. A Dictionary maps keys to values, so that you can have, for instance, a dictionary of people and for each person have their SSN as a key.
A List<T>
is a collection of T. You can get individual items by using the index, the Find method, or simply LINQ (since it implements IEnumerable<T>).
2/5
Top Answer
Answered Jun 02, 2017 · 131 votes
Some basic differences can be written in short:
MVC:
Traditional MVC is where there is a
- Model: Acts as the model for data
- View : Deals with the view to the user which can be the UI
- Controller: Controls the interaction between Model and View, where view calls the controller to update model. View can call multiple controllers if needed.
MVP:
Similar to traditional MVC but Controller is replaced by Presenter. But the Presenter, unlike Controller is responsible for changing the view as well. The view usually does not call the presenter.
MVVM
The difference here is the presence of View Model. It is kind of an implementation of Observer Design Pattern, where changes in the model are represented in the view as well, by the VM. Eg: If a slider is changed, not only the model is updated but the data which may be a text, that is displayed in the view is updated as well. So there is a two-way data binding.
3/5
Top Answer
Answered Apr 22, 2009 · 16 votes
This is a question that is dependent upon the particular C++ compiler implementation you are using. The double type can be either 4 or 8 bytes according to the C++ standard. Most compilers do use 8 bytes though. Here are the closest representations
- 4 bytes: float
- 8 bytes: double
Reference: http://msdn.microsoft.com/en-us/library/cc953fe1.aspx
4/5
Top Answer
Answered Nov 17, 2021 · 8 votes
what is the use of IEnumerator function
IEnumerator there isn't a function, it's a return type. C# doesn't have functions either (but I know what you mean) - in C# we call them methods.
IEnumerator being so called implies it is an interface, so any class that implements the IEnumerator interface can be returned by this method
In practice in this use it seems that it's actually more of a hack than intending to provide the true intent of an enumerator, which is to step-by-step rifle through(or generate) a collection of things.
When you use a yield return statement within a method "some magic happens" whereby it's not a return in the classic sense, but creates a facility whereby the code can resume from where it left off (calling for the next item out of the returned enumerator will cause the code to resume from after the yield, with all the state it had before, rather than starting over).
If you look at the MSDN example for yield:
public class PowersOf2{ static void Main() { // Display powers of 2 up to the exponent of 8: foreach (int i in Power(2, 8)) { Console.Write("{0} ", i); } } public static System.Collections.Generic.IEnumerable<int> Power(int number, int exponent) { int result = 1; for (int i = 0; i < exponent; i++) { result = result * number; yield return result; } } // Output: 2 4 8 16 32 64 128 256}
The loop is controlled by i; if this wasn't a yield return then this wouldn't function as intended (it couldn't return an enumerator for a start but we'll leave that out). Suppose it was just a normal return, the loop would never loop at all; the code would enter, start the loop, hit the return, and just return a number one time and all memory of where the loop was would be forgotten.
By making it a yield return, an enumerator is returned instead, and a small set of "saved state" is set up whereby the loop can remember the current value of i - each time you ask for the next value, the code resumes where it left off from (ie just after the yield), the loop goes round again and a different value is yielded. This continues up to the max of course.. at which point the returned enumerator says it has no more items
You could yield forever, too.. If the code can never escape the loop then it will yield/generate forever
In this case you have to use yield return new WaitForSeconds because that's how WaitForSeconds is intended to work. Yielding gives up an enumerator to the calling method, which is then free to enumerate it. From the docs it looks like this is deliberately done on the next frame, so using yield (perhaps repeatedly) is a way of arranging a block of code that occurs across several frames without having some sort of external state management that remembers where the process is up to and a wordy
- if state = 1 then close the door and add 1 to the state,
- else if state = 2 then light the torch and add 1
- else if state = 3 ...".
You can just
- yield,
- close the door,
- yield,
- light the torch,
- yield ..
Can't we do this by this process
Sure, looks reasonable; look at the clock 100 times a second and if 0.5 seconds have passed since you first looked at the clock, spawn the obstacles
I'd imagine (never used Unity; don't profess to know anything about it other than having read the docs for this one function) that your Update loop has a lot more to be getting on with, so handing a process off to a dedicated wait-then-do is more efficient than spending all your time looking at a clock and carrying out a potentially complicated calc to work out if you should do something; most things in life that start out as poll-every-x-milliseconds benefit from being switched to an "if the event occurs, react to it" way of working
5/5
stackoverflow.com › questions › 78534138c# - How to addForce to a GetComponent<Rigidbody2D>() of ...
stackoverflow.com › questions › 785341386 hours ago · public Rigidbody2D peaRB; // Start is called before the first frame update. void Start() peaRB = GetComponent<Rigidbody2D>(); // Update is called once per frame. void Update() public void MovePea() peaRB.AddForce(Vector2.right * 3, ForceMode2D.Impulse); public GameObject FruitOne;
Searches related to what is dictionary in c# terms