Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm totally new to programming and I am wondering; when would be a good time
to use an array or an indexer? I want to know what types of applications
would make good use of arrays or indexers. There seems to be other ways of
doing the jobs of the two and less confusing.
The books I read don't provide good examples of situations when I would need
an array or indexer. I don't really need a definition of them as I already
have that. I just need to know what which well known apps have uses for
arrays and indexer?
This is the first time I've ever heard the term "smart array" in C#
No one uses the phrase "smart array" not that I heard of.
http://social.msdn.microsoft.com/Forums/en-US/3b56248f-1ff6-41aa-a0cd-35735e802e13/what-is-indexer-for-in-c
no one calls indexers smart array except the guy in the linked forum post.
indexers used for stuff like Dictionaries and Hashtables
In general I think arrays are a pain to manage, and I don't use them very often. As a programmer, you have to be completely responsible for the length of an array.
string[] str1 = new string[10];
When you add and remove items from the array, you have to be aware of all times where in the array you are.
Personally I avoid arrays when possible and use strongly typed collection, like List
List<string> str1 = new List<string>();
Then I can use the built-in methods to manage the items without having to be as concerned with managing the fine details.
str1.Add("Hello!");
str1.Add("Hello again!");
str1.RemoveAt(0);
Console.WriteLine(str1[0]); //Hello again!
I think answer to why indexer are known as smart array in c#? question is they are not.
Post on MSDN forum does not prove anything. There is nothing about smart array on MSDN documentation. You should follow official feature naming, which is indexer or indexed property. That's the name you can find on
Specification: chapter 10.9 Indexers
An indexer is a member that enables an object to be indexed in the same way as an array. Indexers are declared using indexer-declarations:
Documentation: Indexers (C# Programming Guide)
Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I would like to know what is more optimal, A number N of private integer variables, or a single array containing N integer values.
When you use an array, that is a plus indirection. The array will be allocated at a separate part of the memory, so when you first access it, your code first obtains its address, then it is able to read out its content. It also needs some indexing, but that is done extremly fast by the CPU. However, .NET is a safe environment and it will do a check whether you use a valid array index. It adds additional time.
When you use separate variables, these will be encompassed by your object instance and no indirection is needed. Also, no index bound check is needed.
Moreover, you cannot name nicely the Nth element of an array, but you can give good names for individual variables. So your code will be readable.
As others mentioned, you shouldn't do this kind of optimalizations, the compiler/jitter take care of it. The compiler knows several common use cases and has optimialization strategy for that. If you start doing tricky things, the compiler will not recognize your intention and cannot make the optimalization for you.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have been learning collection and generics and understood that generics were introduced to
provide more type safe containers and to improve the application performance by reducing number of boxing/unboxing.
But, If that was the case, why does the framework allows to create generics collection of type system.objects which can take any other type?
List<object> listObj = new List<object>();
listObj.Add(new Program1());
listObj.Add(new Program2());
Thanks in advance.
Yes the collection is List<T> is object not a type? object is a type like any other (being the base type doesn't make it any different) and therefor can be used as the type argument for generic collections, there is nothing wrong with that, just don't do it when you know what the specific type is.
I would say: why not. A list of objects, in which form you want it is totally legit. I personally don't want the framework to tell me what to do. It is fine to do suggestions, but prevent this code? No.
If you want a list of things that are neither the same type, nor share a base class, nor share an interface, then you need List<Object>.
You very rarely need this.
But if you pursue the thought that List<Object> should be disallowed because it encourages unnecessary and dangerous downcasts, and anyway with generics you can just specify the types actually can handle, shouldn't you then go all the way and disallow the use of Object entirely?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Original Questions: I know the question sounds pretty "thin", since generic classes (interfaces) and collections go hand in hand. Out of curiosity and a desire to 'cover all the bases' ... are there uses for these generics other than as collections?
The response is that there are too many possibilities to make for a good thread, so let me try to clarify the question because I ( and probably others) will definitely benefit.
My revised question is:
What are applications of instantiated generics (not methods!) in addition to collections? So, now I know there are many ... however, classified by use... what are they?
A concise format for answers is:
Use: Short description or example
(ie) Collections: The generic allows for collections of objects and with a where T: constraint gives access to methods on all members of the collection. (link or reference).
I'm really eager to hear responses.
You can create not only generic types but also generic methods. Though the most common use of generics is for creating collections they are also used for many other purposes such as containers or algorithms.
class Point<T>
{
T x;
T y;
};
class Math<T>
{
T Add(T a, T b);
};
You should also have a look at this discussion: What is cool about generics, why use them?.
I've used generics for a "EventHandler" (with a restriction on the generic that the parameter implemented my BaseEvent class) when sending events via WCF to another piece of the system.
As the comments note, the answer is unequivocally yes. You use generics whenever multiple types (and ideally all types) should have the same behavior (and occasionally state). Collections are an easy example of this, but there are many, many other situations where this holds true and generics are a good choice.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
With dynamic we pretty much have a dynamic pointer, but not exactly a dynamic object. The true dynamic object in C# is the ExpandoObject, but that is a really unknown class for most of people. The expando allows creating and removing members at runtime, much like a hash (similar to JavaScript).
Why the ExpandoObject goodness was implemented in a separate class rather than just being, let's say, implemented as a feature of anonymous types?
Maybe that wouldn't be a good move because the lacking of type-safety? Or maybe due the (DLR) overhead involved?
Because anonymous types have other very important feature - they provide you compile time type safety.
And because dynamic and anonymous types are just different concepts. The first one gives you ability to dispatch object members at runtime, the second lets you create statically typed objects with some base functionality (equality, hashcode, etc) without creating corresponding POCO classes. Why should they be implemented in the same way then?
btw. I use them quite a lot and really rarely needed to use dynamic to deal with them. Are you sure you're using these language features correctly?
Update
I think that's very important part of anonymous types tutorial:
If you must store query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'm learning c# , and I reached the LinkedList<T> type and I still need to know more, like when should I use it, how do I create one, how do I to use it. I just want information.
If any one knows a good article about this subject, or if you can show me some examples with explanation, such as how to create, how to add and remove, and how to deal with nodes and elements.
Thanks in advance. I really enjoy asking questions around here with all the pros answering and helping.
[EDIT] Changed reference to LinkedList<T> instead of "array linkedlist." I think this was what was meant based on the context.
You can find more information on LinkedList<T> at MSDN, including an example of how to create and use one. Wikipedia has a reasonable article on linked lists, including their history, usage, and some implementation details.
Linked is a collection to store sequences of values of the same type, which is a frequent task, e.g. to represent a queue of cars waiting at a stoplight.
There are many different collection types like linked list, array, map, set etc. Which one to use when depends on their properties, e.g.:
do you need some type of ordering?
is the container associative, storing key-value pairs like a dictionary?
can you store the same element twice?
performance measures - how fast is e.g. inserting, removing, finding an element? This is usually given in Big-O notation, telling you how the time required scales with the number of elements in the collection.
Memory footprint and layout. This also can affect performance, due to good/bad locality.
This collection class implements a doubly linked list. It allows you to quickly determine the immediate sibling for a specified item in the collection. Removing an item from the collection automatically resizes it so that it does not leave any gaps.
For more info on LinkedList class, check out LinkedList at MSDN.
Do you know what a standard Linked List is? It's like one of those (doubly linked) but using .NET Generics to allow you to easily store any Type inside of it.
Honestly, I don't use it, I prefer the more basic List or Dictionary.
For more info on Linked Lists, check out wikipedia. As for generics, there are tons of articles here and at MSDN.
linklist are collections.. they can be use as replacements for arrays.. they can dynamically grow in size and has special helper methods that can help the development or the problem solving be faster.. try to view its methods and properties to understand more.
linklist is a generic collection.. meaning can used to declare type safety declarations..