Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

[C#]Sta su reference ???

[es] :: .NET :: .NET Desktop razvoj :: [C#]Sta su reference ???

Strane: 1 2

[ Pregleda: 12261 | Odgovora: 25 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Shadowed
Vojvodina

Član broj: 649
Poruke: 12850



+4784 Profil

icon Re: [C#]Sta su reference ???09.07.2007. u 18:26 - pre 204 meseci
To je to ;)
 
Odgovor na temu

Valerij Zajcev

Član broj: 40886
Poruke: 1374
*.dynamic.sbb.co.yu.



+2 Profil

icon Re: [C#]Sta su reference ???09.07.2007. u 19:29 - pre 204 meseci
Ne ne sad mi je sve jasno samo sam morao da procitam sve o pokazivacima iz Ritchijeve knjizice :) Sve vreme nisam video primenu zasto bih kog djavola radio sve ovo ako moze i na drugi nacin, nesrecom nisam primetio da u primerima nema y = promeniX(x); vec samo promeniX(x);
Svejedno hvala svima :D
 
Odgovor na temu

Java Beograd
Novi Beograd

Član broj: 11890
Poruke: 9513
*.dip0.t-ipconnect.de.



+10254 Profil

icon Re: [C#]Sta su reference ???10.07.2007. u 08:50 - pre 204 meseci
Bingo
OTPOR blokadi ulica, OTPOR blokiranom Beogradu, OTPOR blokiranoj Srbiji
 
Odgovor na temu

Valerij Zajcev

Član broj: 40886
Poruke: 1374
*.dynamic.sbb.co.yu.



+2 Profil

icon Re: [C#]Sta su reference ???17.09.2007. u 20:21 - pre 202 meseci
Reference tipovi (klasa recimo) u metod se prosledjuje po referenci, a ne po vrednosti. Znaci kada objekat ode u metod pokazuje na istu adresu kao i ranije ok. Ali zasto se onda u nekim slucajevima reference tip prosledjuje sa kljucnom reci "ref" ako vec ide u metod kao referenca?
 
Odgovor na temu

android~paranoid

Član broj: 81947
Poruke: 211
212.200.179.*



Profil

icon Re: [C#]Sta su reference ???17.09.2007. u 22:28 - pre 202 meseci
Citat:
Valerij Zajcev: Ali zasto se onda u nekim slucajevima reference tip prosledjuje sa kljucnom reci "ref" ako vec ide u metod kao referenca?


http://msdn2.microsoft.com/en-us/library/14akc2c7(VS.71).aspx

Code:
// cs_ref.cs
using System;
public class MyClass 
{
   public static void TestRef(ref char i) 
   {
      // The value of i will be changed in the calling method
      i = 'b';
   }

   public static void TestNoRef(char i) 
   {
      // The value of i will be unchanged in the calling method
      i = 'c';
   }

   // This method passes a variable as a ref parameter; the value of the 
   // variable is changed after control passes back to this method.
   // The same variable is passed as a value parameter; the value of the
   // variable is unchanged after control is passed back to this method.
   public static void Main() 
   {
   
      char i = 'a';    // variable must be initialized
      TestRef(ref i);  // the arg must be passed as ref
      Console.WriteLine(i);
      TestNoRef(i);
      Console.WriteLine(i);
   }
}

Output

b
b



Citat:
mmix: Pa posto u NETu ne postoji prosledjivanje objekata po vrednosti, taj deo rasprave je besmislen.


Citat:
2.6.1.2 Reference types
If a type consumes significant memory resources, then a reference type provides more benefits over a
value type. Reference types (including objects, interfaces, and pointers) are so called because they
contain references to heap-based objects and can be null. These types are passed by reference,
meaning that when you pass such an object into a function, an address of or pointer to the object is
passed—not a copy of the object, as in the case of a value type. Since you are passing a reference,
the caller will see whatever the called function does to your object. The first benefit here is that a
reference type can be used as an output parameter, but the second benefit is that you don't waste
extra resources because a copy is not made. If your object is large (consuming lots of memory), then
reference types are a better choice.


Zvuci kao da imas izbor, a verovatno nemas :), pod objektom se moze smatrati i value type podataka pa onda u tom slucaju nije veliki za kopiranje.
Mislim, šta reći !
 
Odgovor na temu

Valerij Zajcev

Član broj: 40886
Poruke: 1374
*.dynamic.sbb.co.yu.



+2 Profil

icon Re: [C#]Sta su reference ???17.09.2007. u 23:20 - pre 202 meseci
Code:

static void Main()
    {
        string myString = "original";
        func(ref myString);
        string mySecondString = "originalTwo";
        funcTwo(mySecondString);
        Console.WriteLine();
        Bomb myBomb = new Bomb("tomahawk", "SQ101");
        bombFunc(ref myBomb);
        Console.ReadLine();
    }
    public static void bombFunc(ref Bomb par)
    {
        par.air = "airsabre";
        par = new Bomb();  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<!!!
    }
    public static void func(ref string par)
    {
        par = "changed";
    }
    public static void funcTwo(string par)
    {
        par = "changedTwo";
    }

Citat:

Passing value types by reference--as demonstrated above--is useful, but ref is also useful for passing reference types. This allows called methods to modify the object to which the reference refers because the reference itself is being passed by reference. The following sample shows that when a reference type is passed as a ref parameter, the object itself can be changed.

@android~paranoid hvala na linku pomoglo je kolko tolko. Prvi deo koda tu mi je sve jasno sta se desava ali mene buni samo ova crvena recenica i onaj deo koda kada saljem (MyBomb), a on onda uradi (par = new Bomb();), sta podrazumeva pod tim da je ceo objekat promenjen, jer ocigledno je da ne moram da ga saljem po referenci ako cu samo clanove da menjam, jer to moze i bez toga.

\\edit: kad sam procitao svoj text shvatio sam :)

[Ovu poruku je menjao Valerij Zajcev dana 18.09.2007. u 00:35 GMT+1]
 
Odgovor na temu

[es] :: .NET :: .NET Desktop razvoj :: [C#]Sta su reference ???

Strane: 1 2

[ Pregleda: 12261 | Odgovora: 25 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.