-3

Is it possible to return value from list or a array without brackets?

For example i have List of integers:

List<int> myIntegers = new List<int>();
myIntegers.Add(1);
myIntegers.Add(2);
myIntegers.Add(3);

int newValue = myIntegers; //normally i should use myIntegers[0]

i want myIntegers to return the first value from list but i want to use it without brackets. Is there a way?

Thanks. M.

Dan J
  • 16,319
  • 7
  • 50
  • 82
  • 4
    Just wondering: What is the reason?! – Kirill Polishchuk Oct 27 '14 at 21:53
  • Are you open to suggestions that add, for example, a function call onto `myIntegers`, or are you only searching for a (non-existent) solution to getting the `myIntegers` identifier to return the value of the first item in the list? – Dan J Oct 27 '14 at 21:53
  • It's as easy as using the `IEnumerable.First()` Extension Method which makes the call look like `myIntegers.First()` but why so much hate for the indexer? – Justin Niessner Oct 27 '14 at 21:53
  • 1
    Why do you want to use the list without brackets? On its own that request doesn't make much sense. What problem are you trying to solve? – shf301 Oct 27 '14 at 21:53
  • 1
    You might want to get familiar with Intellisense.... it will tell you all kinds of neat things.... – Erik Funkenbusch Oct 27 '14 at 21:54
  • 2
    On its own this really doesn't make a lot of sense, unless your `[`,`]` keys are stuck. Btw if you are on a Czech keyboard, you can use `Right Alt`+`F`,`G` for the brackets. Though I find it easier to just switch to English ... – vesan Oct 27 '14 at 21:58
  • if this is one of those ridiculous homework assigments, you can do that by creating a wrapper class for `List` and define an implicit conversion from your type to `int`. – Selman Genç Oct 27 '14 at 21:58
  • No it has nothing to do with keyboard :-), it seems Selman22 is close to solution, any example how to code it? – Miroslav Krajcir Oct 27 '14 at 22:02

2 Answers2

1

As you wrote it, it will never compile. However, if you really have a thing against [], then yes. You can use First() (or its companion, FirstOrDefault()), or just ElementAt(0).

First and FirstOrDefault can take a predicate, which is often useful, FirstOrDefault returns default(T) if there is no first element that matches the predicate, and ElementAt does the exact same thing as the [].

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • I know it will never compile, i want to use exactly the "myIntegers" without anything added to it. As it seems i have to use some custom class and not class List. But i have no idea how to code it. – Miroslav Krajcir Oct 27 '14 at 21:55
  • 1
    @Miroslav How would you expect it to know which item to return if you were only assigning the name of the List object to an int? It defeats the purpose of using a List... – eddie_cat Oct 27 '14 at 21:56
  • @MiroslavKrajcir Technically, it wouldn't be impossible to write that object (by allowing some *very* weird casting). But in reality, you *don't* want to do that! You shouldn't be able to assign a `List` to an `int`. Indexing makes it obvious what you are trying to do. – BradleyDotNET Oct 27 '14 at 21:57
  • 1
    I don't see how you can have an identifier that represents both a reference to the collection and simultaneously represents the first item in the collection. – Dan J Oct 27 '14 at 21:57
  • Would the downvoter care to comment? – BradleyDotNET Oct 27 '14 at 21:58
1

If you subclass List<T>, you could potentially do this with an implicit conversion to T. Although, what you are describing is sort of a weird data structure, and not really idiomatic c#. Probably best not to call it a list.

BnWasteland
  • 2,109
  • 1
  • 18
  • 14
  • 2
    Or better, don't do it at all... that assignment would just be *weird*. It is the way to get what the OP wants though. – BradleyDotNET Oct 27 '14 at 22:00
  • 2
    @BradleyDotNET agreed. I would yell at somebody if I saw this at work. – BnWasteland Oct 27 '14 at 22:01
  • Just to clarify, i saw something similar in one program (and i am sure it was coded in c#) the user can get a value by adresing some index like myIntegers[0] but he can also use myIntegers and it also returns the first value in the array or list. – Miroslav Krajcir Oct 27 '14 at 22:06
  • @MiroslavKrajcir Just because someone did it before doesn't make it right now. I'm sure you could find production code out there with `ArrayList` as well, or even `goto`, but that doesn't mean you should use it today. Check the link if you want to see how to do this. – BradleyDotNET Oct 27 '14 at 22:11
  • I'd be very interested in seeing such a program, but even if it's possible it's not a very good idea. Is there any reason you're after this solution other than a desire to see if it's possible? – Dan J Oct 27 '14 at 22:12
  • Here is the description from their manual: – Miroslav Krajcir Oct 27 '14 at 22:49
  • todaysClose = instrument.close todaysClose = instrument.close[0] yesterdaysClose = instrument.close[1] When a property does not support '[ ]' like the instrument examples above, those properties are not series and contain values of the current bar location, or their values are static for an instrument. For example, instrument.bigPointValue is a static value for each instrument, and instrument.bar is reference value that provides the current record number of that instrument. – Miroslav Krajcir Oct 27 '14 at 22:49
  • @MiroslavKrajcir Here's what the manual said: **When a property does not support `[]` ... those properties are not series**. In other words, they aren't a collection, and so can't be indexed. If its a collection (like `List` you should **have** to index in order to get an element. – BradleyDotNET Oct 27 '14 at 23:04
  • No, see the example once it is used with on once without brackets. – Miroslav Krajcir Oct 28 '14 at 04:05