Home All Groups Group Topic Archive Search About

CLR and accessible features

Author
14 Jan 2006 9:19 AM
pradeep_TP
Hi all,

I was reading about MSIL and i came to know that there are some features of
CLR that can be only accessed by programming in IL. I want to know what are
these features which cannot be accessed by languages like c# or vb.net

Thanks
pradeep_tp

Author
14 Jan 2006 10:16 AM
Willy Denoyette [MVP]
"pradeep_TP" <pradee***@discussions.microsoft.com> wrote in message
news:1F7FC440-E89A-4487-9E26-FA8AFF442214@microsoft.com...
| Hi all,
|
| I was reading about MSIL and i came to know that there are some features
of
| CLR that can be only accessed by programming in IL. I want to know what
are
| these features which cannot be accessed by languages like c# or vb.net
|
| Thanks
| pradeep_tp

What exactly do you mean with "features of the CLR", MSIL allows you to do
things which aren't possible/allowed to do in higher level languages, but as
far as the CLR goes there is nothing that IL does what can't be done in C#
for instance.

Willy.
Author
14 Jan 2006 12:17 PM
pradeep_TP
Hi willy

By saying "features of CLR" , I mean  CLR's facilties. As you have
mentioned, what are those things which are allowed/possisble only through
MSIL.

thanks
pradeep_tp

Show quote
"Willy Denoyette [MVP]" wrote:

>
> "pradeep_TP" <pradee***@discussions.microsoft.com> wrote in message
> news:1F7FC440-E89A-4487-9E26-FA8AFF442214@microsoft.com...
> | Hi all,
> |
> | I was reading about MSIL and i came to know that there are some features
> of
> | CLR that can be only accessed by programming in IL. I want to know what
> are
> | these features which cannot be accessed by languages like c# or vb.net
> |
> | Thanks
> | pradeep_tp
>
> What exactly do you mean with "features of the CLR", MSIL allows you to do
> things which aren't possible/allowed to do in higher level languages, but as
> far as the CLR goes there is nothing that IL does what can't be done in C#
> for instance.
>
> Willy.
>
>
>
Author
14 Jan 2006 1:43 PM
Naveen
One thing that can be done using IL but cannot be done using C# or
VB.NET is that the access modifier protected internal. The protected
internal in C# or VB.NET is an "OR" operator that is it is either
protected or internal. In IL it possible to declare "protected or
internal" and "protected and internal".
Author
14 Jan 2006 2:48 PM
Willy Denoyette [MVP]
"Naveen" <naveensrinivasan.nav***@gmail.com> wrote in message
news:1137246211.285671.322840@g44g2000cwa.googlegroups.com...
| One thing that can be done using IL but cannot be done using C# or
| VB.NET is that the access modifier protected internal. The protected
| internal in C# or VB.NET is an "OR" operator that is it is either
| protected or internal. In IL it possible to declare "protected or
| internal" and "protected and internal".
|

However, it can be done in managed C++.

Willy.
Author
14 Jan 2006 3:05 PM
Naveen
Yes, I know it can be done in Managed C++. But the intial question was
what can be done in IL that cannot be done in C# or VB.NET.
Author
14 Jan 2006 3:42 PM
Willy Denoyette [MVP]
"Naveen" <naveensrinivasan.nav***@gmail.com> wrote in message
news:1137251127.667583.67570@f14g2000cwb.googlegroups.com...
| Yes, I know it can be done in Managed C++. But the intial question was
| what can be done in IL that cannot be done in C# or VB.NET.
|

Ok, some nitpickng maybe, but the OP said  "...by languages like C# and
VB.NET....", which IMO has a broader scope than "in C# and VB.NET".
But I was expecting this when I replied, that's why I said in my other reply
that it's hard to tell what can be done in IL that can't be done in higher
level languages.
The point is, if you realy need something that can't be done in your
language of choice, are you going to do it in IL when you can do it in
another higher level language? Not an easy question to answer either.


Willy.
Author
14 Jan 2006 2:48 PM
Willy Denoyette [MVP]
Oh, I see, but I'm afraid I can't answer this question because I don't know
all languages that target the CLR.
If I restrict myself to C#, VB.NET and managed C++, all I can come up with
is:
- managed exports, that is the possibility to expose managed functions to
unmanaged callers.
- overloads of methods with covariant return types

Willy.

Show quote
"pradeep_TP" <pradee***@discussions.microsoft.com> wrote in message
news:CF8B5B0D-0E9E-44E7-94E6-EC1A20A3FF4E@microsoft.com...
| Hi willy
|
| By saying "features of CLR" , I mean  CLR's facilties. As you have
| mentioned, what are those things which are allowed/possisble only through
| MSIL.
|
| thanks
| pradeep_tp
|
| "Willy Denoyette [MVP]" wrote:
|
| >
| > "pradeep_TP" <pradee***@discussions.microsoft.com> wrote in message
| > news:1F7FC440-E89A-4487-9E26-FA8AFF442214@microsoft.com...
| > | Hi all,
| > |
| > | I was reading about MSIL and i came to know that there are some
features
| > of
| > | CLR that can be only accessed by programming in IL. I want to know
what
| > are
| > | these features which cannot be accessed by languages like c# or vb.net
| > |
| > | Thanks
| > | pradeep_tp
| >
| > What exactly do you mean with "features of the CLR", MSIL allows you to
do
| > things which aren't possible/allowed to do in higher level languages,
but as
| > far as the CLR goes there is nothing that IL does what can't be done in
C#
| > for instance.
| >
| > Willy.
| >
| >
| >
Author
14 Jan 2006 3:33 PM
Jon Skeet [C# MVP]
Willy Denoyette [MVP] <willy.denoye***@telenet.be> wrote:
> Oh, I see, but I'm afraid I can't answer this question because I don't know
> all languages that target the CLR.
> If I restrict myself to C#, VB.NET and managed C++, all I can come up with
> is:
> - managed exports, that is the possibility to expose managed functions to
> unmanaged callers.
> - overloads of methods with covariant return types

Do any of the languages allow you to modify a boxed value in place? For
instance, if I had a struct:

public struct Foo
{
    public int x;
}

and put that into an ArrayList, in C# I certainly wouldn't be able to
modify the value in the list - I'd have to overwrite it with a new
boxed value after unboxing and modifying the other one, if you see what
I mean.

My guess is that it's available in C++, but it doesn't hurt to know
these things...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
14 Jan 2006 4:32 PM
Willy Denoyette [MVP]
Show quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1e332a3085fa441498ccdf@msnews.microsoft.com...
| Willy Denoyette [MVP] <willy.denoye***@telenet.be> wrote:
| > Oh, I see, but I'm afraid I can't answer this question because I don't
know
| > all languages that target the CLR.
| > If I restrict myself to C#, VB.NET and managed C++, all I can come up
with
| > is:
| > - managed exports, that is the possibility to expose managed functions
to
| > unmanaged callers.
| > - overloads of methods with covariant return types
|
| Do any of the languages allow you to modify a boxed value in place? For
| instance, if I had a struct:
|
| public struct Foo
| {
|    public int x;
| }
|
| and put that into an ArrayList, in C# I certainly wouldn't be able to
| modify the value in the list - I'd have to overwrite it with a new
| boxed value after unboxing and modifying the other one, if you see what
| I mean.
|
| My guess is that it's available in C++, but it doesn't hurt to know
| these things...
|

Don't know about other languages, but AFAIK the managed C++ language has no
way to do it either and the compiler won't generate code to modify
"in-place" (AFIAK), at least not in a 'safe' context.
In an unsafe context, I guess there is, but the question is; if it's more
efficient than an unbox/modify/box trip?

Willy.
Author
14 Jan 2006 4:52 PM
Jon Skeet [C# MVP]
Willy Denoyette [MVP] <willy.denoye***@telenet.be> wrote:
> Don't know about other languages, but AFAIK the managed C++ language has no
> way to do it either and the compiler won't generate code to modify
> "in-place" (AFIAK), at least not in a 'safe' context.
> In an unsafe context, I guess there is, but the question is; if it's more
> efficient than an unbox/modify/box trip?

I seem to remember testing it in a sample program by modifying some IL
generated from C# - no need for unsafe code, it's just an IL unbox
without a copy. I think it made a fairly significant difference, but
only in a situation which is limited by this kind of operation
happening a lot. Of course, generics solve this problem in a rather
neater way anyway, if you're able to use 2.0 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
14 Jan 2006 5:14 PM
Willy Denoyette [MVP]
Show quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1e333cc1382d6d3898cce0@msnews.microsoft.com...
| Willy Denoyette [MVP] <willy.denoye***@telenet.be> wrote:
| > Don't know about other languages, but AFAIK the managed C++ language has
no
| > way to do it either and the compiler won't generate code to modify
| > "in-place" (AFIAK), at least not in a 'safe' context.
| > In an unsafe context, I guess there is, but the question is; if it's
more
| > efficient than an unbox/modify/box trip?
|
| I seem to remember testing it in a sample program by modifying some IL
| generated from C# - no need for unsafe code, it's just an IL unbox
| without a copy. I think it made a fairly significant difference, but
| only in a situation which is limited by this kind of operation
| happening a lot. Of course, generics solve this problem in a rather
| neater way anyway, if you're able to use 2.0 :)
|

Jon,
Sorry, I was only thinking about higher level languages, I'm sure it can be
done in IL, however, I'm not sure it passes the verifier, remember (tweaked)
IL is per definition 'unsafe' (well non-verifiable), Peverify is your friend
here :-).

Willy.
Author
14 Jan 2006 5:40 PM
Mattias Sjögren
>Do any of the languages allow you to modify a boxed value in place?

This works for me:

// Old syntax
__value struct Foo
{
public:
    Foo(int x) { this->x = x; }
    virtual String * ToString() { return __box(x)->ToString(); }
    int x;
};

int main()
{
    Object *o = __box(Foo(42));
    dynamic_cast<Foo*>(o)->x = 123;
    Console::WriteLine(o);
    return 0;
}

// New syntax, compiles with /clr:safe
value struct Foo
{
public:
    Foo(int x) { this->x = x; }
    virtual String^ ToString() override { return x.ToString(); }
    int x;
};

int main()
{
    Object ^o = Foo(42);
    dynamic_cast<Foo^>(o)->x = 123;
    Console::WriteLine(o);
    return 0;
}


To add to the list of things you can't do in C#, VB or C++, I don't
think any of them ever generates the tail. or cpblk IL instructions.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
15 Jan 2006 5:11 AM
pradeep_TP
Hi all,

Mattias has given some interesting information. I will now find more about 
tail and cpblk.

I thank you all for your help. I hope i get to hear more about this from
more experts. I also would like to express my special thanks to john skeet. I
have read many of his blogs and find it all very interesting. I feel honoured
to see your reply on this thread john :).

thanks
pradeep

Show quote
"Mattias Sjögren" wrote:

> >Do any of the languages allow you to modify a boxed value in place?
>
> This works for me:
>
> // Old syntax
> __value struct Foo
> {
> public:
>     Foo(int x) { this->x = x; }
>     virtual String * ToString() { return __box(x)->ToString(); }
>     int x;
> };
>
> int main()
> {
>     Object *o = __box(Foo(42));
>     dynamic_cast<Foo*>(o)->x = 123;
>     Console::WriteLine(o);
>     return 0;
> }
>
> // New syntax, compiles with /clr:safe
> value struct Foo
> {
> public:
>     Foo(int x) { this->x = x; }
>     virtual String^ ToString() override { return x.ToString(); }
>     int x;
> };
>
> int main()
> {
>     Object ^o = Foo(42);
>     dynamic_cast<Foo^>(o)->x = 123;
>     Console::WriteLine(o);
>     return 0;
> }
>
>
> To add to the list of things you can't do in C#, VB or C++, I don't
> think any of them ever generates the tail. or cpblk IL instructions.
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
>
Author
15 Jan 2006 1:21 PM
Willy Denoyette [MVP]
But, this doesn't modify the boxed value in place, it unboxes before
modifying.

Take a look at the IL....
....
IL_0013: isinst Foo
IL_0018: unbox Foo
IL_001d: ldc.i4.s 123
IL_001f: stfld int32 Foo::x
....

Willy.


Show quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:e2dVtHTGGHA.1424@TK2MSFTNGP12.phx.gbl...
| >Do any of the languages allow you to modify a boxed value in place?
|
| This works for me:
|
| // Old syntax
| __value struct Foo
| {
| public:
| Foo(int x) { this->x = x; }
| virtual String * ToString() { return __box(x)->ToString(); }
| int x;
| };
|
| int main()
| {
| Object *o = __box(Foo(42));
| dynamic_cast<Foo*>(o)->x = 123;
| Console::WriteLine(o);
| return 0;
| }
|
| // New syntax, compiles with /clr:safe
| value struct Foo
| {
| public:
| Foo(int x) { this->x = x; }
| virtual String^ ToString() override { return x.ToString(); }
| int x;
| };
|
| int main()
| {
| Object ^o = Foo(42);
| dynamic_cast<Foo^>(o)->x = 123;
| Console::WriteLine(o);
| return 0;
| }
|
|
| To add to the list of things you can't do in C#, VB or C++, I don't
| think any of them ever generates the tail. or cpblk IL instructions.
|
|
| Mattias
|
| --
| Mattias Sjögren [C# MVP]  mattias @ mvps.org
| http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| Please reply only to the newsgroup.
Author
15 Jan 2006 1:44 PM
Mattias Sjögren
Willy,

>it unboxes before modifying.

Right, but to quote the spec for the unbox instruction:

"Unlike box, which is required to make a copy of a value type for use
in the object, unbox is not required to copy the value type from the
object. Typically it simply computes the address of the value type
that is already present inside of the boxed object."


If a copy that was modified, Console::WriteLine(o) would print the
original value 42.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
15 Jan 2006 4:21 PM
Willy Denoyette [MVP]
Show quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:uAgMaodGGHA.1552@TK2MSFTNGP10.phx.gbl...
| Willy,
|
| >it unboxes before modifying.
|
| Right, but to quote the spec for the unbox instruction:
|
| "Unlike box, which is required to make a copy of a value type for use
| in the object, unbox is not required to copy the value type from the
| object. Typically it simply computes the address of the value type
| that is already present inside of the boxed object."
|
|
| If a copy that was modified, Console::WriteLine(o) would print the
| original value 42.
|
|
| Mattias
|
| --
| Mattias Sjögren [C# MVP]  mattias @ mvps.org
| http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| Please reply only to the newsgroup.

You are right of course (wasn't awake I guess when I replied), Unbox returns
a managed pointer the home of Foo::x) and the 'stfld' stores the value into
the home of x. This is exactly what C# doesn't allow. Therefore, the
following C# equivalent won't compile...

      object o = new Foo(42);
      ((Foo)o).x = 123;

Willy.
Author
16 Jan 2006 9:45 PM
Richard Grimes
pradeep_TP wrote:
> Hi all,
>
> I was reading about MSIL and i came to know that there are some
> features of CLR that can be only accessed by programming in IL. I
> want to know what are these features which cannot be accessed by
> languages like c# or vb.net

One feature is that all the high level languages I know about insist
that array indexes are 0-based, the CLI allows you to specify the range
of the index (for a multi-dimensional array), and so it does not have to
be 0-based.

Richard

AddThis Social Bookmark Button