473,786 Members | 2,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

type of expression in C# 2.0?

In C# 2.0, given an arbitrary expression and the types of the
variables referenced in it, how do I find the expression type? For
example,
int x;
float y;
what is the type of x+y?

If I had values for x and y, I could do (x+y).GetType() . But I don't,
I just have their types. Default values won't work because values
that don't cause the expression to raise an error might be very hard
to come by.

I can dynamically generate
public static object f(int x, float y)
{
object o = x+y;
return o;
}
and use reflection (MethodInfo->MethodBody->LocalVariables ) to find
that o is of type System.Object, and I'll get a typecheck error if x+y
isn't allowed, but I don't see how to get the type of x+y given that
it is allowed. If I had C# 3.0, I think I could do "var o = x+y;"
then use reflection to find the type of o, but I've only got C# 2.0.

It's conceivable that x+y returns different types depending on the
values of x and y, so x+y truly has no type.

Oct 4 '07 #1
3 1749
I just read an incredible chapter in a forthcoming C# book on Lamba
Expressions and Expression Trees.

What you describe is, from what I understand, almost exact how the compiler
would build an expression tree. I don't think, in C# 2.0, the tools exist
for you to do it.

Hopefully the author of that book will chime in with the real answer soon,
and we'll all become a bit more educated...

--
Chris Mullins

<bo*********@bu rtleburtle.netw rote in message
news:11******** **************@ 19g2000hsx.goog legroups.com...
In C# 2.0, given an arbitrary expression and the types of the
variables referenced in it, how do I find the expression type? For
example,
int x;
float y;
what is the type of x+y?

If I had values for x and y, I could do (x+y).GetType() . But I don't,
I just have their types. Default values won't work because values
that don't cause the expression to raise an error might be very hard
to come by.

I can dynamically generate
public static object f(int x, float y)
{
object o = x+y;
return o;
}
and use reflection (MethodInfo->MethodBody->LocalVariables ) to find
that o is of type System.Object, and I'll get a typecheck error if x+y
isn't allowed, but I don't see how to get the type of x+y given that
it is allowed. If I had C# 3.0, I think I could do "var o = x+y;"
then use reflection to find the type of o, but I've only got C# 2.0.

It's conceivable that x+y returns different types depending on the
values of x and y, so x+y truly has no type.

Oct 4 '07 #2
Chris Mullins [MVP - C#] <cm******@yahoo .comwrote:
I just read an incredible chapter in a forthcoming C# book on Lamba
Expressions and Expression Trees.

What you describe is, from what I understand, almost exact how the compiler
would build an expression tree. I don't think, in C# 2.0, the tools exist
for you to do it.

Hopefully the author of that book will chime in with the real answer soon,
and we'll all become a bit more educated...
I wish :)

The way the compiler works it out is by knowing the various addition
operations available, e.g.

int op_Addition(int x, int y)
long op_Addition(lon g x, long y)
double op_Addition(dou ble x, double y)
float op_Addition(flo at x, float y)

(etc)

It then applies normal overload resolution. The first two of the above
overloads isn't valid, but the bottom two are, having performed
implicit conversions. The conversion from float to float is better than
from float to double, and the conversion from int to float is as good
as the conversion from int to double, so the bottom overload is used.

But basically you need to have this information available to you...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 4 '07 #3
<bo*********@bu rtleburtle.netw rote:

<snip>
It's conceivable that x+y returns different types depending on the
values of x and y, so x+y truly has no type.
No, if x+y are variables, x+y will always have the same type,
determined at compile time.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
5639
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
13
1637
by: fctk | last post by:
hello is it possible to determine the type of an expression? something like operator sizeof but for types... for example: typeof(5.0) --> returns "double" typeof(5+7) --> returns "int"
669
26222
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
15
2247
by: Alex Vinokur | last post by:
Why does one need to use two kinds of sizeof operator: * sizeof unary-expression, * sizeof (type-name) ? Their behavior seem not to be different (see an example below). ------ C++ code ------ #include <iostream> using namespace std;
3
1595
by: mario | last post by:
Hi! First of all, sorry for my English, it's not my native tongue. Anyway, here we go: suppose I create a class that handles my own custom text strings. No, suppose I create TWO such classes, which differ e.g. in the characters mappings and in the implementation, etc... Necessary operators are overloaded, so that the following example works as one may intuitively expect it to work:
6
1561
by: Protoman | last post by:
Read this code I found on the Internet: Object o,*oo; // Some objects int (Object::*ptr_to_o_func)(double); // ptr_to_obj_func is a pointer to a // member function of Object oo = getObjectPtr(); ptr_to_o_func = (//some boolean condition) ?
1
23435
by: urkel | last post by:
Hi everyone, I critically need help to solve this problem related to pointer in C++ Basically, I have a C/C++ program "retardselfenerg" calling a Fortran 90 subroutine "surfGF4.f90". i am so doubtful if my definitions of pointer variables and their use in calling function are correct. #include<bla bla bla......h> #include<bla bla bla.h> double surfGF4_(double*, double*, double*, double*, double*, double*, double*, double*, double*); ...
5
3271
by: not_a_commie | last post by:
It seems that the only way to construct a struct from a type is to use Activator.CreateInstance. Is that true? Can anyone improve (performance-wise) upon this function below: /// <summary> /// Create an object of the given type /// A default constructor is required to be successful. /// A failure will return null. /// </summary>
2
2485
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.