`
cyhcheng
  • 浏览: 57937 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

自己学习中碰到的问题

    博客分类:
  • C#
阅读更多

using System;
using System.Linq;

namespace StudyLinq
{
    class Program
    {
        public int Id { get; set; }

        //public string Name { get; }//需要使用下面的方法替换本行代码,否则出现错误:'StudyLinq.Program.Name.get' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.
        private string _Name = "默认值";

        public string Name
        {
            get { return _Name; }
        }



        public readonly bool Sex = true;//只能在构造函数或直接中初始化,不能在其他方法中初始化
        public Program()
        {
            Id = 100;
            Sex = false;
            //Name = "没有setter方法,我不能被赋值!";

        }

        public void test()
        {
            //Sex = true; //只读变量,只能在构造函数或直接中初始化,不能在其他方法中初始化
        }


        static void Main(string[] args)
        {
            StudyEntities studyEntities = new StudyEntities();
            var students = studyEntities.Students.ToList();
            Console.WriteLine("数据库中存储的学生读过的图书有:");
            foreach (var student in students)
            {
                Console.Write(student.Name + "读过的图书有:");
                var books = student.Books;
                foreach (var book in books)
                {
                    Console.Write(book.Name + ", ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("货币格式化:{0, -30:C4}", 50000.999); //Aligned left
            Console.WriteLine("货币格式化:{0, 30:c4}", 50000.999); //Aligned right
            Console.WriteLine("16进制数:{0:x}", 20);
            Console.WriteLine("默认效果:{0:G}", 16666);
            Console.WriteLine("默认效果:{0:G4}", 16666);
            Console.WriteLine("默认效果:{0:G6}", 16666.88);

            string switchStr = "a";

            switch (switchStr)
            {
                case "a":
                    {
                        Console.WriteLine("aaaaa");
                        break;
                    }
                case "b":
                    {
                        Console.WriteLine("bbbb");
                        break;
                    }
                default:
                    {
                        Console.WriteLine("other");
                        break;
                    }
            }

            


            Console.WriteLine("Press Enter key to exit");

            Console.ReadLine();
        }
    }

    class Employee
    {
        public string LastName; // Call this field 0.
        public string FirstName; // Call this field 1.
        public string CityOfBirth; // Call this field 2.
        public string this[int index] // Indexer declaration
        {
            set // Set accessor declaration
            {
                switch (index)
                {
                    case 0: LastName = value;
                        break;
                    case 1: FirstName = value;
                        break;
                    case 2: CityOfBirth = value;
                        break;
                    default: // (Exceptions in Ch. 11)
                        throw new ArgumentOutOfRangeException("index");
                }
            }
            get // Get accessor declaration
            {
                switch (index)
                {
                    case 0: return LastName;
                    case 1: return FirstName;
                    case 2: return CityOfBirth;
                    default: // (Exceptions in Ch. 11)
                        throw new ArgumentOutOfRangeException("index");
                }
            }
        }
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics