Wednesday, 25 May 2016

.net iii module 4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace StudentLibrary
{
    public class StudentManager
    {
        //private property
        private List<Student> Students { get; set; }
        //private Student[] Students { get; set; }

        //default constructor
        public StudentManager()
        {
            Students = new List<Student>();
            //Students = new Student[] { };
            //add some test records
            Students.Add(new Student { Id = 1, FirstName = "John", LastName = "Doe", Phone = "403-555-4560" });
            Students.Add(new Student { Id = 2, FirstName = "Jane", LastName = "Smith", Phone = "403-555-1122" });
            Students.Add(new Student { Id = 3, FirstName = "Ben", LastName = "King", Phone = "403-555-3790" });
            Students.Add(new Student { Id = 4, FirstName = "Sarah", LastName = "Carson", Phone = "403-555-9900" });
            Students.Add(new Student { Id = 5, FirstName = "Ken", LastName = "Hunter", Phone = "403-555-5311" });
        }

        //public method returns studebnts as IList
        public IList<Student> GetAll()
        {
            return Students;
        }

        public IList StudentLookupList()
        {

            //foreach(var std in Students)
            //{
            //    stds.Add( new { StudentID = std.Id, StudentName = std.FullName } );
            //}

//return a new collection students<studentID, StudentName>, not Students<ID, First.., Last.., Phone>
            var students = from s in Students
                           select new { StudentID = s.Id, StudentName = s.FullName };

            return students.ToList();
        }

        public Student Find(int id)
        {
            //foreach(var s in Students)
            //{
            //    if (s.Id == id)
            //        return s;
            //}
            //return null;
            return Students.SingleOrDefault(s => s.Id == id);
        }
    }
}


c# select clause
https://msdn.microsoft.com/en-us/library/bb384087.aspx

c# tolist()
https://msdn.microsoft.com/en-us/library/bb342261(v=vs.110).aspx

c# singleordefault

Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

https://msdn.microsoft.com/en-us/library/bb342451(v=vs.100).aspx

c# ilist vs list

If you are exposing your class through a library that others will use, you generally want to expose it via interfaces rather than concrete implementations. This will help if you decide to change the implementation of your class later to use a different concrete class. In that case the users of your library won't need to update their code since the interface doesn't change.

http://stackoverflow.com/questions/5322545/difference-between-list-and-ilist

---------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtensionMethods
{
    public static class StringStuff
    {
        public static string ToPhoneNumber(this string value)
        {
            switch (value.Length)
            {
                case 7:
                    return value.Insert(3, "-");
                case 10:
                    return value.Insert(3, "-").Insert(7, "-");
                case 11:
                    return value.Insert(1, "-").Insert(5, "-").Insert(9, "-");
                default:
                    return value;
            }
        }

        public static int ToInteger(this string value)
        {
            return int.Parse(value);
        }
    }
}

c# extention

call method from other class 
MyClass myClass = new MyClass();
int i = myClass.Foo();
rather than
MyClass myClass = new MyClass();
int i = Foo(myClass);

No comments:

Post a Comment