Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Common Intermediate Language
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Object-oriented concepts=== CIL is designed to be object-oriented. One may create objects, call methods, and use other types of members, such as fields. Every [[Method (programming)|method]] needs (with some exceptions) to reside in a class. So does this static method: <syntaxhighlight lang="csharp"> .class public Foo { .method public static int32 Add(int32, int32) cil managed { .maxstack 2 ldarg.0 // load the first argument; ldarg.1 // load the second argument; add // add them; ret // return the result; } } </syntaxhighlight> The method Add does not require any instance of Foo to be declared because it is declared as static, and it may then be used like this in C#: <syntaxhighlight lang="csharp"> int r = Foo.Add(2, 3); // 5 </syntaxhighlight> In CIL it would look like this: <syntaxhighlight lang="csharp"> ldc.i4.2 ldc.i4.3 call int32 Foo::Add(int32, int32) stloc.0 </syntaxhighlight> ====Instance classes==== An instance class contains at least one [[Constructor (object-oriented programming)|constructor]] and some [[Instance (computer science)|instance]] members. The following class has a set of methods representing actions of a Car-object. <syntaxhighlight lang="csharp"> .class public Car { .method public specialname rtspecialname instance void .ctor(int32, int32) cil managed { /* Constructor */ } .method public void Move(int32) cil managed { /* Omitting implementation */ } .method public void TurnRight() cil managed { /* Omitting implementation */ } .method public void TurnLeft() cil managed { /* Omitting implementation */ } .method public void Brake() cil managed { /* Omitting implementation */ } } </syntaxhighlight> ====Creating objects==== In C# class instances are created like this: <syntaxhighlight lang="csharp"> Car myCar = new Car(1, 4); Car yourCar = new Car(1, 3); </syntaxhighlight> And those statements are roughly the same as these instructions in CIL: <syntaxhighlight lang="csharp"> ldc.i4.1 ldc.i4.4 newobj instance void Car::.ctor(int, int) stloc.0 // myCar = new Car(1, 4); ldc.i4.1 ldc.i4.3 newobj instance void Car::.ctor(int, int) stloc.1 // yourCar = new Car(1, 3); </syntaxhighlight> ====Invoking instance methods==== Instance methods are invoked in C# as the one that follows: <syntaxhighlight lang="csharp"> myCar.Move(3); </syntaxhighlight> As invoked in CIL: <syntaxhighlight lang="csharp"> ldloc.0 // Load the object "myCar" on the stack ldc.i4.3 call instance void Car::Move(int32) </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)