Advertisement
Guest User

readonly c#

a guest
Nov 28th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication3
  6. {
  7.     public class ReadOnlyTest
  8.     {
  9.         class SampleClass
  10.         {
  11.             public int x;
  12.             // Initialize a readonly field
  13.             public readonly int y = 25;
  14.             public readonly int z;
  15.  
  16.             public SampleClass()
  17.             {
  18.                 // Initialize a readonly instance field
  19.                 z = 24;
  20.             }
  21.  
  22.             public SampleClass(int p1, int p2, int p3)
  23.             {
  24.                 x = p1;
  25.                 y = p2;
  26.                 z = p3;
  27.             }
  28.         }
  29.  
  30.         static void Main()
  31.         {
  32.             SampleClass p1 = new SampleClass(11, 21, 32);   // OK
  33.             Console.WriteLine("p1: x={0}, y={1}, z={2}", p1.x, p1.y, p1.z);
  34.             SampleClass p2 = new SampleClass();
  35.             p2.x = 55;   // OK
  36.             Console.WriteLine("p2: x={0}, y={1}, z={2}", p2.x, p2.y, p2.z);
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement