Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C# have an equivalent to decltype in C++11?

Being already familiar with C++ and after trying some of the new features C++11 offers, I decided to become more familiar with C#.

As expected, programming principles are similar, but some of the features are different. The differences and similarities are what I am looking after and therefore I decided to ask if C# has an equivalent to decltype in C++11?

int x = 4;
decltype(x) y = 16;

In the example above 'var' would work just fine, so here is an example of when decltype is needed. If I only conditionally call a function then I need to declare the variable that will hold its result without using 'var', as shown here:

var pendingProcessData = trace.UseProcesses();
// Only request CPU scheduling data when it is actually needed, to avoid
// unecessary trace processing costs. Unfortunately this means that the
// 
IPendingResult<ICpuSchedulingDataSource> pendingSchedulingData = null;
if (showCPUUsage)
    pendingSchedulingData = trace.UseCpuSchedulingData();

trace.Process();

ICpuSchedulingDataSource schedulingData = null;
if (showCPUUsage)
    schedulingData = pendingSchedulingData.Result;

With decltype I could say something like this:

var pendingProcessData = trace.UseProcesses();
// Only request CPU scheduling data when it is actually needed, to avoid
// unecessary trace processing costs. Unfortunately this means that the
// 
decltype(trace.UseCpuSchedulingData()) pendingSchedulingData = null;
if (showCPUUsage)
    pendingSchedulingData = trace.UseCpuSchedulingData();

trace.Process();

decltype(pendingSchedulingData.Result) schedulingData = null;
if (showCPUUsage)
    schedulingData = pendingSchedulingData.Result;

This extends the awesomeness of 'var' and would have saved me from tracking down what the concrete types are. To be clear, I don't care what the types of pendingSchedulingData and schedulingData are so forcing me to figure that out and mention it in the code has real cost but no value.

like image 375
Newborn In Yah Avatar asked Sep 13 '14 22:09

Newborn In Yah


People also ask

What does %d do in C?

%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.

What is && operator in C?

The && (logical AND) operator indicates whether both operands are true. If both operands have nonzero values, the result has the value 1 . Otherwise, the result has the value 0 . The type of the result is int . Both operands must have an arithmetic or pointer type.

What does -> mean in C?

An Arrow operator in C/C++ allows to access elements in Structures and Unions. It is used with a pointer variable pointing to a structure or union.


1 Answers

In C# there's no equivalent to decltype.

Maybe type inference would be the nearest feature to decltype:

int x = 4;
var y = x + 1;

But there's no actual syntax feature that would allow to declare a storngly-typed variable with the type inferred from other given variable or expression.

like image 101
Matías Fidemraizer Avatar answered Sep 20 '22 06:09

Matías Fidemraizer