473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1669
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******************@bgtnsc05-news.ops.worldnet.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.worldnet.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
"constraints" 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
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...
3
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...
1
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...
3
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...
0
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...
3
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
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...
4
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...
4
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.