473,545 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why need constraints?

Why are constraints needed? In C++, an attempt to use a non-existing method
will cause a compiler error. Isn't that true in C# also?
May 14 '06 #1
4 1674
I am not sure to what constraints are you referring (maybe database
constraints?)

But the answer to your question is yes, C# compiler will not compile a code
that would be calling a nonexistent method.

"Ian Lazarus" <no****@nowhere .com> wrote in message
news:qW******** **********@bgtn sc05-news.ops.worldn et.att.net...
Why are constraints needed? In C++, an attempt to use a non-existing
method will cause a compiler error. Isn't that true in C# also?

May 14 '06 #2

"Ian Lazarus" <no****@nowhere .com> a écrit dans le message de news:
qW************* *****@bgtnsc05-news.ops.worldn et.att.net...
Why are constraints needed? In C++, an attempt to use a non-existing
method will cause a compiler error. Isn't that true in C# also?


If you're speaking about Generics constraints, there are IMHO 2 main reasons
for that :

- Generics syntax and semantic is checked before the generic is instanciated
(generics are instanciated at run time, not at compile time). Therefore,
when the compiler "sees" and checks a generic' source, it couldn't make any
diagnostics if it hadn't an idea on what the instanciation type will look
like.

- Many people claims that C++ templates are hard to use because the
"constraint s" in the code (the instanciation type must provide such and such
method for example) are not clearly expressed, but are messed up inside the
template implementation. This make is very difficult to use templates as a
"black box" library because very often the library user must dive deep
inside the library source details to understand the requirements on the
instanciation types he provides to the library.
This is also what make the C++ template compiler error messages notoriously
very hard to read and understand.

Btw, there are several proposals for the next C++ standard (C++0x) to add
the notion of "Concepts", which are very similar to C# generics constraints.
See
http://www.cs.rice.edu/~jgs3847/pubs...epts_cpp0x.pdf
for an example of what it may looks like.

Arnaud
MVP - VC
May 14 '06 #3
"Ian Lazarus" <no****@nowhere .com> wrote:
Why are constraints needed? In C++, an attempt to use a non-existing method
will cause a compiler error. Isn't that true in C# also?


In C++, the constraints on the type arguments are implicit based on the
way the type is used in the body of the template, and don't get fully
validated until you instantiate them by passing type arguments to them
at compile time - because it's only then that the type argument can be
tested for all the constraints expected of it. (That can lead to
confusing errors.)

..NET generics aren't instantiated at compile time - instead they're
instantiated at runtime. Amongst other things, that means that the whole
bodies of generic classes and methods get fully compiled to MSIL object
code without the type arguments being known. In order to do that, there
must be some reference to (for example) the MethodRef or MethodDef for
method calls, and the FieldRef or FieldDef for field accesses (these are
the metadata tokens used in MSIL to refer to fields and methods). Where
are these methods and fields to come from if the type arguments aren't
known? They come from the generic type argument constraints, which can
specify an inheritance relationship, reference or value type, default
constructor and implemented interfaces.

-- Barry
May 14 '06 #4
The answer lies in the difference between C# generics and C++
templates. Although they appear similar on the surface, underneath
they're quite different.

C++ templates are, more or less, simply a matter of text expansion: the
compiler simply plugs the type(s) that you supply into the template and
then compiles the resulting text. (It's a bit more complex than that,
but that's the nut of it.) So, as you pointed out, any attempt to call
methods that don't exist _on the types you supplied_ results in a
compiler error during the template expansion.

C# generics are different. They're supported by the runtime. This has
several advantages, including that you can use Reflection to create new
types from generic types at runtime, whereas in C++ templates don't
actually exist at runtime: they're purely compiler constructs. As well,
it solves certain problems with versioning: if you use a C# generic
from another assembly, you get the version of that generic supplied by
the assembly that is loaded at runtime, not necessarily the version
with which you compiled the client assembly.

Anyway, C# generics are different. The generic itself is compiled into
the .NET assembly, and then the generic is expanded into (a) concrete
type(s) at runtime, on demand, by the JIT.

However, this means that the C# compiler can't let you call any method
you want to from a generic, and issue compiler errors if the method is
missing. Because the generic is built into a concrete type at runtime,
not at compile time, the compiler has no way of knowing for which
type(s) the generic will be built. So, it has no way of knowing which
method calls are legal and which are not.

This is where constraints come in. Constraints give the C# compiler
additional information about the type(s) with which the generic can be
built by placing restrictions upon the types that can be supplied at
runtime. The compiler can then check method calls and property
references, etc, against the generic constraints, knowing that the
runtime will not allow generic expansion on types that do not meet the
constraints.

BTW, take this babbling with a grain of salt since I'm not actually
using .NET 2.0 yet. :-)

May 14 '06 #5

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

Similar topics

18
1794
by: Tad Marko | last post by:
Howdy! I'm trying to get my head around Python and I've come to realize that there are a lot of idioms in Python that are a bit different than in other languages. I have a class similar to what I've included below. The only difference is that in the real class, I have even more mandatory attributes. Too many to specify at once on a...
3
3848
by: D | last post by:
Database 1 is corrupted. Database 2 was created by dba but none of the primary or foreign key constraints were ported over. TOAD won't let me export. I will try ErWin next. What is the best way to recover this information if the views are corrupted and through errors? thanks, Lowly developer
1
3875
by: Robin Tucker | last post by:
I'm considering adding domain integrity checks to some of my database table items. How does adding such constraints affect SQL Server performance? For example, I have a simple constraint that restricts a couple of columns to having values within the values assigned in my application by an enumeration: (( >= 0 and <=3) and ( >= 0 and <=...
3
25038
by: Tim | last post by:
I have spent the last 2-3 hours trying to find a way to just list the constraints for a given table (this includes referential - foriegn keys, not just check constraints). I know how to create them, but can I see them, no way. Howz it done people, let me in on the secret. I tried SYSIBM.SYSCHECKDEP but this doesnt show foriegn key...
0
1421
by: BobTheDatabaseBoy | last post by:
i've Googled some this morning, but to my surprise, i don't find any offering (for fee or open source), which would integrate with, say Jakarta Struts, to provide the UI edits from cataloged constraints/triggers. there have been some threads, even recently, dealing with with the subject of where the "true" repository of data integrity...
3
6049
by: Marek Berkan | last post by:
Hi, I have a problem with deffering constraints with db2. It was explained five year ago at this same group...
11
3975
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I looked at the files in the Gyro download but I couldn't find any mention of constraints. Can anyone enlighten me what the current status is and what we...
4
3819
by: Peter | last post by:
I am interested in informed feedback on the use of Constraints, Primary Keys and Unique. The following SQL statement creates a Bands tables for a database of bookings Bands into Venues, where the rule of the business is that only band plays on the one night. The SQL statement prevents a Band name being repeated (as it is Unique). Similar...
4
21957
by: Bobby Edward | last post by:
I have an xsd dataset. I created a simple query called GetDataByUserId. I can preview the data fine! I created a very simple BLL function that calls it and returns a datatable. When I run the code I get.... ======================= Server Error in '/MyCompanyMyProject' Application.
0
7415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7675
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. ...
0
7928
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...
0
7775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5997
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5344
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...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.