Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C# anonymous types redundant in C# 7

Since C# 7 introduces value tuples, is there a meaningful scenario where they are better suited than tuples?

For example, the following line

collection.Select((x, i) => (x, i)).Where(y => arr[y.i].f(y.x)).ToArray();

makes the following line

collection.Select((x, i) => new {x, i}).Where(y => arr[y.i].f(y.x)).ToArray();

redundant.

What would be the use case where one is better used over the other (for either performance reasons or optimization)?

Obviously, if there is a need for more than six fields, tuples cannot be used, but is there something a bit more nuanced to it?

like image 615
Igor Ševo Avatar asked Jun 30 '17 16:06

Igor Ševo


People also ask

Are C and C++ the same?

C++ was developed by Bjarne Stroustrup in 1979. C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming. C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language. C is a subset of C++.

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Is C or C+ Better?

Compared to C, C++ has significantly more libraries and functions to use. If you're working with complex software, C++ is a better fit because you have more libraries to rely on. Thinking practically, having knowledge of C++ is often a requirement for a variety of programming roles.

What is C used for?

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 ...

What is the difference between C and C++?

Both languages were originally implemented as source-to-source compilers; source code was translated into C, and then compiled with a C compiler. The C++ programming language (originally named "C with Classes ") was devised by Bjarne Stroustrup as an approach to providing object-oriented functionality with a C-like syntax.

What does Area C stand for?

Area C ( Hebrew: שטח C ‎; Arabic: منطقة ج ‎) is an Oslo II administrative division of the West Bank, defined as "areas of the West Bank outside Areas A and B".

What is K&R C?

The version of C that it describes is commonly referred to as " K&R C ". As this was released in 1978, it is also referred to as C78. The second edition of the book covers the later ANSI C standard, described below. K&R introduced several language features:

What is the origin of C language?

The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 by Dennis Ritchie and Ken Thompson, incorporating several ideas from colleagues. Eventually, they decided to port the operating system to a PDP-11.


1 Answers

There are various differences between anonymous types and C# 7 tuples, which may or may not make one more appropriate than the other in certain situations:

  • C# 7 tuples are ValueTuple<>s. That means they are value types while anonymous types are reference types.
  • Tuples allow static typing at compile time since they are a type that can be expressed explicitly. As such, you can use them as method arguments, return types, etc.
  • Members of an anonymous type are actual properties that exist on the type. Tuple items are fields.
  • The properties of an anonymous type have an actual name, while the fields on a tuple are just named ItemN (for numbers N). The labels are just metadata information that is mostly used by the compiler, and is not persisted with the actual tuple object.
  • Because creating an anonymous type actually creates a type under the hood, you have a level of type safety with them. Since tuples are just generic containers with applied type arguments, you do not have full type safety with them. For example an (int, int) tuple for a size would be fully compatible to an (int, int) tuple for a position, while anonymous types are closed off completely.
  • As Jon Skeet mentioned, the C# 7 tuple syntax is currently not supported in expression trees.
like image 114
poke Avatar answered Sep 19 '22 17:09

poke