示例#1
0
        public static void main()
        {
            ReferencesExample re = new ReferencesExample();

            // Returns a reference type, caller can access member - gaping hole in your class encapsulation.
            var modifiableList = re.getListOfNamesUsingModifiableReferenceType();

            Console.WriteLine("Before modification: ");
            modifiableList.Add("Saori");

            Console.WriteLine("After modification: ");
            modifiableList.ForEach(Console.WriteLine);

            // Returns object that implements an interface that does not have any modification methods.
            // Using interfaces, you can choose to expose only ther methods and properties you want clients to use.
            var enumerableList = re.getListOfNamesUsingReadonlyInterface();

            // Uses extention method created above.
            enumerableList.ForEach(Console.WriteLine);

            // Returns read-only collection that does not support modification methods.
            var readonlyList = re.getListOfNamesUsingReadOnlyCollection();

            Console.WriteLine(readonlyList.Count);
        }