コード例 #1
0
        public static void DoProc()
        {
            using (PeopleContext context = new PeopleContext())
            {
                context.People4.Add(new Person4()
                {
                    Id = 1, Name = "Steave",
                });
                context.People4.Add(new Person4()
                {
                    Id = 2, Name = "Bill",
                });
                context.People4.Add(new Person4()
                {
                    Id = 3, Name = "Mark",
                });
                context.SaveChanges();
            }

            using (PeopleContext context = new PeopleContext())
            {
                Person4 person = context.People4.SingleOrDefault(p => p.Id == 3);
                Console.WriteLine(person.Name);
            }
        }
コード例 #2
0
        public static void DoProc()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Person4));
            string        xml;

            using (StringWriter stringWriter = new StringWriter())
            {
                Person4 p = new Person4()
                {
                    Id   = 1,
                    Name = "Taro",
                    Age  = 20,
                };
                serializer.Serialize(stringWriter, p);
                xml = stringWriter.ToString();
            }

            Console.WriteLine(xml);

            using (StringReader stringReader = new StringReader(xml))
            {
                Person4 p = (Person4)serializer.Deserialize(stringReader);
                Console.WriteLine(p.ToString());
            }
        }
コード例 #3
0
        public static void DoProc()
        {
            //Dictionary<string, IEnumerable<Tuple<Type, int>>> data =
            //    new Dictionary<string, IEnumerable<Tuple<Type, int>>>();

            // 戻り値の方が自明な場合、左辺はvarにすると可読性が向上する
            var implicitData = new Dictionary <string, IEnumerable <Tuple <Type, int> > >();

            // object initialization syntax
            var person = new Person4()
            {
                Id   = 1,
                Name = "test",
                Age  = 10,
            };

            // the same syntax can be used when creating collections.
            var persons = new List <Person4>()
            {
                new Person4()
                {
                    Id   = 2,
                    Name = "test2",
                    Age  = 20,
                },
                new Person4()
                {
                    Id   = 3,
                    Name = "test3",
                    Age  = 30,
                },
            };

            // lambda expression (anonymous method)
            Func <int, int> myDelegate = delegate(int x)
            {
                return(x * 2);
            };

            // lambda expression is syntax sugar, for using Func and Action.

            Action <Person4> test = (x) =>
            {
                Console.WriteLine(x.ToString());
            };

            test(person);
        }
コード例 #4
0
ファイル: Example4-73.cs プロジェクト: yadac/MCP70483cs
        public static void DoProc()
        {
            _logger.Debug(DateTime.Now);
            var person = new Person4
            {
                Id   = 1,
                Name = "Taro",
                Age  = 20
            };

            IFormatter formatter = new BinaryFormatter();

            using (Stream stream = new FileStream("data.bin", FileMode.Create))
            {
                formatter.Serialize(stream, person);
            }

            using (Stream stream = new FileStream("data.bin", FileMode.Open))
            {
                var dPerson = (Person4)formatter.Deserialize(stream);
                Console.WriteLine(dPerson.ToString());
            }
        }