Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Program
- {
- public static void Main()
- {
- X x = new X(){x = 0};
- //Posto je y property klase Y, nije potrebno praviti konstruktor koji ce primati int kao parametar, jer je y property.
- Y y = new Y() {y = 0};
- IncrementClass(x);
- Console.WriteLine(x.x); // Bice ispisana 1, zato sto se klasa proseldjuje po referenci.
- IncrementStruct(y);
- Console.WriteLine(y.y); //Bice ispisana 0, zato sto se struktura prosledjuje po vrednosti tj pravi se kopija prosledjenog parametra.
- IncrementStruct(ref y); //Ukoliko se koristi ref kljucna rec, prosledice se stvarni parametar koji se nece kopirati
- Console.WriteLine(y.y); //Bice ispisana 1
- }
- public static void IncrementClass(X x){
- x.x++;
- }
- public static void IncrementStruct(Y y){
- y.y++;
- }
- public static void IncrementStruct(ref Y y){
- y.y++;
- }
- public class X{
- public int x{get;set;}
- }
- public struct Y{
- public int y {get;set;}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement