Data File: (Data.txt) lines represent width height
5
6 9
7 2
4 4
C# Code:
var list = new List<dynamic>();
using (var sr = new StreamReader("Data.txt", Encoding.UTF8))
{
list = sr.ReadToEnd().Split('\n').Select(r =>
{
var split = r.Split(' ');
var len = split.Length;
return new {
w = len > 0 ? int.Parse(split[0].Trim()) : 0,
h = len > 1 ? int.Parse(split[1].Trim()) : 0
} as dynamic;
}).ToList();
}
int Area = list.Sum(r => r.h * r.w);
The example works as is. I had to do a few undesired things to make it work.
First I had to declare the list to avoid the using scope - since I do not have a typed dimension object I made the type dynamic (var list = new List<dynamic>()
).
The undesirable part is casting the anonymous object to a dynamic (as dynamic
). Otherwise I get
Cannot implicitly convert type
System.Collections.Generic.List<AnonymousType#1>
toSystem.Collections.Generic.List<dynamic>
Why do I get this error? I know a dynamic can hold an anonymous type, so is this a problem with the ToList()
extension and dynamics?
I need to be able to access the anonymous list items outside of the using statement, as in the last line that calculates area.
Solution: I went with dtb's answer. It avoided the use of a using statement and dynamics all together. Thank you all for the input!
var list =
(from line in File.ReadLines("Data.txt")
let parts = line.Split(' ')
let width = int.Parse(parts[0])
let height = parts.Length > 1 ? int.Parse(parts[1]) : 0
select new { width, height }).ToList();
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
You can use File.ReadLines to avoid the StreamReader.
IEnumerable<dynamic> query =
from line in File.ReadLines("Data.txt")
let parts = line.Split(' ')
let width = int.Parse(parts[0])
let height = parts.Length > 1 ? int.Parse(parts[1]) : 0
select new { width, height } as dynamic;
List<dynamic> list = query.ToList();
int area = list.Sum(t => t.width * t.height);
However, as others have pointed out, using dynamic isn't really appropriate here. If you're using the query only within a method, an anonymous instance is good enough. If you want to use the result of the query outside the method, create a small struct or class or use Tuple<T1,T2>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With