Posts

Showing posts from May, 2007

Operatorii is si as

Costul operatorilor as is is
static void Main(string[] args)
{
object obj = new string('s', 1);
IsDemo(obj);
AsDemo(obj);
ExplicitDemo(obj);
} static void IsDemo(object str)
{
string local = null;

if (str is string)
{
local = (string)str;
}
}

.method private hidebysig static void IsDemo(object str) cil managed
{
// Code size 29 (0x1d)
.maxstack 2
.locals init ([0] string local,
[1] bool CS$4$0000)
IL_0000: nop
IL_0001: ldnull
IL_0002: stloc.0
IL_0003: ldarg.0

-- parametrul este incarcat si se testeaze daca este de tip System.String

IL_0004: isinst [mscorlib]System.String
IL_0009: ldnull

-- se incarca pe stiva rezultatul operatiei IL_0004 > IL_0009 (in acest caz e 1)

IL_000a: cgt.un
IL_000c: ldc.i4.0
IL_000d: ceq

-- se calculeaza IL_000a == IL_000a (adica 1 == 0) si se pastreaza rezultatul in loc.1

IL_000f: stloc.1
IL_0010: ldloc.1

-- daca este adevarat (1 == 0) se sare la instru…

Orice este un obiect

using System;

namespace Namespace
{
public interface IObject
{
void MyToString();
}
public class MyObject : IObject
{
public void MyToString(){}
}
public class Program
{
static void SomeFunction(object a, object b, IObject c, MyObject d)
{
Int32 i = (int)a;
MyObject obj_b = b as MyObject;
MyObject obj_bb = (MyObject)b;
IObject iobj_b = (IObject)b;

MyObject obj_c = (MyObject)c;
MyObject obj_cc = c as MyObject;

IObject iobj_d = d as MyObject;
IObject iobj_dd = d;
IObject iobj_ddd = (MyObject)d;
}
static void Main(string[] args)
{
Int32 i = 3;
MyObject myobj = new MyObject();

SomeFunction(i, myobj, myobj, myobj);

Hello World

Sunt Adrian Iftode, programator .Net, cel putin asta fac 8 ore la serviciu. Acest blog va contine lucruri simple legate de programare cu limbajul C#.