public async Task <IActionResult> PutStudent2(int id, Student2 student2) { if (id != student2.Id) { return(BadRequest()); } _context.Entry(student2).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!Student2Exists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Student2> > PostStudent2(Student2 student2) { _context.Students2.Add(student2); await _context.SaveChangesAsync(); return(CreatedAtAction("GetStudent2", new { id = student2.Id }, student2)); }
/// <summary> /// 查询测试 /// </summary> /// <param name="p"></param> /// <param name="d"></param> private void Select(Student2 p, SqlSugarClient db) { //查询第一条 var select1 = db.Queryable <Student>().Single(it => it.id == p.id); var select2 = db.Queryable <Student>().Single(it => it.id == _p.id); //查询分页 id3-id5 var select3 = db.Queryable <Student>().OrderBy(it => it.id).Skip(2).Take(3).ToList(); //查询分页 id4-id6 var select4 = db.Queryable <Student>().OrderBy(it => it.id).ToPageList(2, 3).ToList(); //2表join var join1 = db.Queryable <Student>().JoinTable <School>((st, sc) => st.sch_id == sc.id) .Select <School, V_Student>((st, sc) => new V_Student() { SchoolName = sc.name, id = st.id, name = st.name }).ToList(); //3表join var join2 = db.Queryable <Student>() .JoinTable <School>((st, sc) => st.sch_id == sc.id) .JoinTable <School, Area>((st, sc, a) => sc.AreaId == a.id) .Select <School, Area, V_Student>((st, sc, a) => new V_Student() { SchoolName = sc.name, id = st.id, name = st.name, AreaName = a.name }).ToList(); }
public void Init() { Student2 p = new Student2() { id = 2, name = "张表", isOk = true }; Console.WriteLine("启动Test.Init"); using (var db = SugarDao.GetInstance()) { //初始化数据 InitData(db); //查询测试 Select(p, db); //拉姆达测试 Exp(p, db); //新容器转换测试 SelectNew(p, db); //更新测试 Update(p, db); //删除测试 Delete(p, db); } }
/// <summary> /// 批量更新 /// </summary> /// <param name="p"></param> /// <param name="db"></param> private void Update(Student2 p, SqlSugarClient db) { db.UpdateRange(StudentListUpd); var list = db.Queryable <Student>().ToList(); db.SqlBulkReplace(StudentListUpd2); list = db.Queryable <Student>().ToList(); }
public static void Main(string[] args) { Student2 s2 = new Student2(); s2.id2 = 104; s2.name2 = "Jane Doe \n"; Console.WriteLine(s2.id2); Console.WriteLine(s2.name2); }
private void button2_Click(object sender, EventArgs e) { Student2 stu2 = DDD.getStudentObject(user); Form12 form12 = new Form12(ref DDD, user); string s = stu2.firstName + " " + stu2.middleName + " " + stu2.lastName; form12.ChangeLabelName(s); form12.ShowDialog(); }
//[Route("api/v2/students/{id}")] public IHttpActionResult Get(int id) { Student2 student = students.FirstOrDefault(s => s.ID == id); if (student == null) { return(Content(HttpStatusCode.NotFound, "Student not found.")); } return(Ok(student)); }
public ActionResult DataAnnotation(Student2 model) { //数据注解方式 if (ModelState.IsValid) { ViewBag.name = model.name; ViewBag.email = model.email; } return(View()); }
public override string ToString() { string result = Student1.ToString(); if (Student2 != null) { result += ", " + Student2.ToString(); } return(result); }
/// <summary> /// 删除测试 /// </summary> /// <param name="p"></param> /// <param name="db"></param> private void Delete(Student2 p, SqlSugarClient db) { db.Delete <Student>(it => it.id == 1); db.Delete <Student>(new Student() { id = 2 }); db.Delete <Student, int>(3, 4); db.Delete <Student, int>(it => it.id, 5, 6); var list = db.Queryable <Student>().ToList(); }
public IHttpActionResult GetStudent2(int id) { Student2 student2 = db.Student2.Find(id); if (student2 == null) { return(NotFound()); } return(Ok(student2)); }
private void button1_Click(object sender, EventArgs e) { if (listBox1.SelectedItem != null) { string student = listBox1.SelectedItem.ToString(); Student2 stu = DDD.getStudentObject(student); Form11 form11 = new Form11(ref DDD, student); string s = stu.firstName + " " + stu.middleName + " " + stu.lastName; form11.ChangeLabelName(s); form11.ShowDialog(); } }
public IHttpActionResult PostStudent2(Student2 student2) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Student2.Add(student2); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = student2.IdStudent }, student2)); }
public static void Main() { // This Student uses get/set methods Student s = new Student(); s.SetName("Fred"); Console.WriteLine("Name is " + s.GetName()); // This Student uses properties Student2 s2 = new Student2(); s2.Name = "Maria"; Console.WriteLine("Name is " + s2.Name); }
public IHttpActionResult DeleteStudent2(int id) { Student2 student2 = db.Student2.Find(id); if (student2 == null) { return(NotFound()); } db.Student2.Remove(student2); db.SaveChanges(); return(Ok(student2)); }
public void Init() { Student2 p = new Student2() { id = 2, name = "张表", isOk = true }; Console.WriteLine("启动Test.Init"); using (var db = SugarDao.GetInstance()) { //拉姆达测试 Exp(p, db); } }
public void Init() { OracleConfig.SequenceMapping = new List <SequenceModel>() { new SequenceModel() { ColumnName = "ID", TableName = "STUDENT", Value = "SEQ" }, new SequenceModel() { ColumnName = "ID", TableName = "AREA", Value = "SEQ2" }, new SequenceModel() { ColumnName = "ID", TableName = "SCHOOL", Value = "SEQ3" }, new SequenceModel() { ColumnName = "ID", TableName = "SUBJECT", Value = "SEQ4" } }; Student2 p = new Student2() { id = 2, name = "张表", isOk = true }; Console.WriteLine("启动Test.Init"); using (var db = SugarDao.GetInstance()) { //初始化数据 InitData(db); //查询测试 Select(p, db); //拉姆达测试 Exp(p, db); //新容器转换测试 SelectNew(p, db); //更新测试 Update(p, db); //删除测试 //Delete(p,db); } }
static void Main(string[] args) { Controller controller = new Controller(); Menu menu = new Menu(); MainStudent student1 = new Student1().get(); MainStudent student2 = new Student2().get(); students.AddRange(new List <MainStudent>() { student2, student1 }); main : menu.Main(); int ordger; try { ordger = int.Parse(Console.ReadLine()); switch (ordger) { case 1: Console.Clear(); controller.ShowList(students); break; case 2: Console.Clear(); controller.Show(students); break; case 3: Console.Clear(); controller.Average(students); break; } Console.ReadKey(); goto main; } catch { Console.WriteLine("\n!!!please enter number!!!"); goto main; } }
static void classexample() { Student2 firstStudent = new Student2("John", "Smith", "six"); Student2.StudentCount++; Student2 secondStudent = new Student2("Tom", "Thumb", "two"); Student2.StudentCount++; Console.WriteLine(firstStudent.firstName); Console.WriteLine(secondStudent.firstName); Console.WriteLine(Student2.StudentCount); firstStudent.FirstName = "Hello"; secondStudent.FirstName = "Challo"; firstStudent.displayName(); secondStudent.displayName(); }
public IHttpActionResult PutStudent2(int id, Student2 student2) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != student2.IdStudent) { return(BadRequest()); } Student2 originalStudent = db.Student2.Find(id); if (originalStudent != null) { originalStudent.Career = student2.Career; originalStudent.File = student2.File; originalStudent.FirstName = student2.FirstName; originalStudent.LastName = student2.LastName; originalStudent.IdNumber = student2.IdNumber; originalStudent.StartYear = student2.StartYear; } try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!Student2Exists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
/* * 90分以上是A * 80分以上是B * 70分以上是C * 60分以上是D * 其餘是E */ static void Main(string[] args) { Student a = new Student(); a.Name = "靜香"; a.score = 83; Student b = new Student(); b.Name = "出木杉"; b.score = 90; Student c = new Student(); c.Name = "胖虎"; c.score = 68; Student d = new Student(); d.Name = "葉大雄"; d.score = 17; getGrade(a); getGrade(b); getGrade(c); getGrade(d); ChangeName(a); Console.WriteLine($"a 的名字現在叫 {a.Name}"); ChangeName(b.Name); Console.WriteLine($"b 的名字現在叫 {b.Name}"); ModifyScore(c); Console.WriteLine($"c 的成績現在是 {c.score}"); ModifyScore(d.Name, d.score); Console.WriteLine($"d 的成績現在是 {d.score}"); Student2 e = new Student2("小夫", 75); Console.WriteLine($"{e.Name} 的分數是 {e.Score} ,成績為 {e.Grade}"); Console.WriteLine($"{e.Name} 如果加5分,分數是 {e.Score += 5},成績為 {e.Grade}"); }
/// <summary> /// 拉姆达测试 /// </summary> /// <param name="p"></param> /// <param name="db"></param> private void Exp(Student2 p, SqlSugarClient db) { //解析拉姆达各种情况组合 //基本 var t1 = db.Queryable <Student>().Where(it => it.id == 1).ToList(); var t2 = db.Queryable <Student>().Where(it => it.id == p.id).ToList(); var t3 = db.Queryable <Student>().Where(it => it.id == _p.id).ToList(); var t4 = db.Queryable <Student>().Where(it => it.id == Getp().id).ToList(); var t5 = db.Queryable <Student>().Where(it => it.id == MyCost.p.id).ToList(); //BOOL var t11 = db.Queryable <Student2>("Student").Where(it => it.isOk).ToList(); var t21 = db.Queryable <Student2>("Student").Where(it => it.isOk == MyCost.p.isOk).ToList(); var t31 = db.Queryable <Student2>("Student").Where(it => !it.isOk).ToList(); var t41 = db.Queryable <Student2>("Student").Where(it => it.isOk == true).ToList(); var t51 = db.Queryable <Student2>("Student").Where(it => it.isOk == false).ToList(); var t61 = db.Queryable <Student2>("Student").Where(it => it.isOk == _p.isOk).ToList(); var t71 = db.Queryable <Student2>("Student").Where(it => !it.isOk && !p.isOk == it.isOk).ToList(); var t91 = db.Queryable <Student2>("Student").Where(it => _p.isOk == false).ToList(); var t81 = db.Queryable <Student>().Where(it => it.isOk == false).ToList(); var t111 = db.Queryable <Student2>("Student").Where(it => it.isOk && false).ToList(); //length var c1 = db.Queryable <Student>().Where(c => c.name.Length > 4).ToList(); var c2 = db.Queryable <Student>().Where(c => c.name.Length > _p.name.Length).ToList(); var c3 = db.Queryable <Student>().Where(c => c.name.Length > "aa".Length).ToList(); var c4 = db.Queryable <Student>().Where(c => c.name.Length > Getp().id).ToList(); //Equals var a1 = db.Queryable <Student>().Where(c => c.name.Equals(null)).ToList(); var x = new InsertTest() { }; var x1 = db.Queryable <Student>().Where(c => c.name.Equals(x.int1)).ToList(); var a2 = db.Queryable <Student>().Where(c => c.name.Equals(p.name)).ToList(); var a4 = db.Queryable <Student>().Where(c => c.name.Equals(Getp().name)).ToList(); //Contains var s = db.Queryable <Student>().Where(c => c.name.Contains(null)).ToList(); var s0 = new InsertTest() { }; var s1 = db.Queryable <Student>().Where(c => c.name.Contains(x.v1)).ToList(); var s3 = db.Queryable <Student>().Where(c => c.name.Contains(p.name)).ToList(); var s4 = db.Queryable <Student>().Where(c => c.name.Contains(Getp().name)).ToList(); var s5 = db.Queryable <Student>().Where(c => c.name.StartsWith(Getp().name)).ToList(); var s6 = db.Queryable <Student>().Where(c => c.name.EndsWith(Getp().name)).ToList(); //异常处理测试,防止程序中出现未知错误 try { var e6 = db.Queryable <Student>().Where(c => Getp().name.StartsWith(c.name)).ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { var e6 = db.Queryable <Student>().Where(c => Getp().name.Equals(c.name)).ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { var e6 = db.Queryable <Student>().Where(c => c.name.First() != null).ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); } //组合测试 var z = db.Queryable <Student>().Where(c => (c.name.Equals(Getp().name) || c.name == p.name) && true && c.id > 1).ToList(); var z23 = db.Queryable <Student>().Where(c => !string.IsNullOrEmpty(c.name) || (c.id == 1 || c.name.Contains(p.name))).ToList(); var z2 = db.Queryable <Student>().Where(c => !string.IsNullOrEmpty(c.name) || !string.IsNullOrEmpty(c.name)).ToList(); }
/// <summary> /// 测试select new /// </summary> /// <param name="p"></param> /// <param name="db"></param> private void SelectNew(Student2 p, SqlSugarClient db) { //测试用例 var queryable = db.Queryable <Student>().Where(c => c.id < 10) .Select <V_Student>(c => new V_Student { id = c.id, name = c.name, AreaName = "默认地区", SchoolName = "默认学校", SubjectName = "NET" }); var list = queryable.ToList(); //多表操作将Student转换成V_Student var queryable2 = db.Queryable <Student>() .JoinTable <School>((s1, s2) => s1.sch_id == s2.id) .Where <School>((s1, s2) => s2.id < 10) .Select <School, V_Student>((s1, s2) => new V_Student() { id = s1.id, name = s1.name, AreaName = "默认地区", SchoolName = s2.name, SubjectName = "NET" }); //select new 目前只支持这种写法 var list2 = queryable2.ToList(); //select字符串 转换成V_Student var list3 = db.Queryable <Student>() .JoinTable <School>((s1, s2) => s1.sch_id == s2.id) .Where(s1 => s1.id <= 3) .Select <V_Student>("s1.*,s2.name SchoolName") .ToList(); //新容器转换函数的支持 只支持ObjToXXX和Convert.ToXXX var f1 = db.Queryable <InsertTest>().Select <Student>(it => new Student() { name = it.d1.ObjToString(), id = it.int1.ObjToInt() // 支持ObjToXXX 所有函数 }).ToList(); var f2 = db.Queryable <InsertTest>().Select <Student>(it => new Student() { name = Convert.ToString(it.d1),//支持Convet.ToXX所有函数 id = it.int1.ObjToInt(), sex = Convert.ToString(it.d1), }).ToList(); var f3 = db.Queryable <InsertTest>() .JoinTable <InsertTest>((i1, i2) => i1.id == i2.id) .Select <InsertTest, Student>((i1, i2) => new Student() { name = Convert.ToString(i1.d1), //多表查询例子 id = 1, sex = Convert.ToString(i2.d1), }).ToList(); //Select 外部参数用法 var f4 = db.Queryable <InsertTest>().Where("1=1", new { id = 100 }).Select <Student>(it => new Student() { id = "@id".ObjToInt(), //取的是 100 的值 name = "张三", //内部参数可以直接写 sex = it.txt, sch_id = it.id }).ToList(); var f6 = db.Queryable <InsertTest>() .JoinTable <InsertTest>((i1, i2) => i1.id == i2.id) .Where("1=1", new { id = 100, name = "张三", isOk = true }) //外部传参给@id .Select <InsertTest, Student>((i1, i2) => new Student() { name = "@name".ObjToString(), //多表查询例子 id = "@id".ObjToInt(), sex = i2.txt, sch_id = 1, isOk = "@isOk".ObjToBool() }).ToList(); try { //测试用例 db.Queryable <Student>().Where(c => c.id < 10) .Select <V_Student>(c => new V_Student { id = c.id, name = _p.name, AreaName = "默认地区", SchoolName = "默认学校", SubjectName = "NET" }).ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { //测试用例 db.Queryable <Student>().Where(c => c.id < 10) .Select <V_Student>(c => new V_Student { id = c.id, name = p.name, AreaName = "默认地区", SchoolName = "默认学校", SubjectName = "NET" }).ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { //测试用例 db.Queryable <Student>().Where(c => c.id < 10) .Select <V_Student>(c => new V_Student { id = c.id, name = Getp().name, AreaName = "默认地区", SchoolName = "默认学校", SubjectName = "NET" }).ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { //测试用例 db.Queryable <Student>().Where(c => c.id < 10) .Select <V_Student>(c => new V_Student { id = c.id, name = Getp2(), AreaName = "默认地区", SchoolName = "默认学校", SubjectName = "NET" }).ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { //测试用例 db.Queryable <InsertTest>() .JoinTable <InsertTest>((i1, i2) => i1.id == i2.id) .Select <InsertTest, Student>((i1, i2) => new Student() { name = Getp2(), //多表查询例子 id = i1.int1.ObjToInt(), sex = Convert.ToString(i2.d1), }).ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { //测试用例 db.Queryable <InsertTest>() .JoinTable <InsertTest>((i1, i2) => i1.id == i2.id) .Select <InsertTest, Student>((i1, i2) => new Student() { name = Getp().name, //多表查询例子 id = i1.int1.ObjToInt(), sex = Convert.ToString(i2.d1), }).ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { //测试用例 db.Queryable <InsertTest>() .JoinTable <InsertTest>((i1, i2) => i1.id == i2.id) .Select <InsertTest, Student>((i1, i2) => new Student() { name = Getp().name, //多表查询例子 id = i1.int1.ObjToInt(), sex = Convert.ToString(i2.d1), }).ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { //测试用例 db.Queryable <InsertTest>() .JoinTable <InsertTest>((i1, i2) => i1.id == i2.id) .Select <InsertTest, Student>((i1, i2) => new Student() { name = Getp().name, //多表查询例子 id = i1.int1.ObjToInt() + 1, sex = Convert.ToString(i2.d1), }).ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { //测试用例 db.Queryable <InsertTest>() .JoinTable <InsertTest>((i1, i2) => i1.id == i2.id) .Select <InsertTest, Student>((i1, i2) => new Student() { name = p.name, //多表查询例子 sex = Convert.ToString(i2.d1), }).ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void button1_Click(object sender, EventArgs e) { //Form2 form2 = new Form2(students, admin, faculty, courses, null); bool flag = false; foreach (DataRow row in DDD.FacultyDB.Rows) { if (row.Field <string>(0) == textBox1.Text.ToLower() && row.Field <string>(1) == textBox2.Text) { Faculty2 fac = DDD.getFacultyObject(textBox1.Text.ToLower()); Form5 form5 = new Form5(ref DDD, fac.user); //List<Course> studentsCourses = element.getRegisteredCourses(); //courseBox = new List<string>(); //if (studentsCourses == null) //{ // courseBox.Add("You are not registered for any courses yet!"); //} //else //{ // courseBox.Clear(); // foreach (Course c in studentsCourses) // { // courseBox.Add(c.ToString()); // } //} string s = "Welcome " + fac.firstName + " " + fac.middleName + " " + fac.lastName; form5.ChangeLabelName(s); form5.ShowDialog(); flag = true; form5.Refresh(); break; //Faculty2 faculty = DDD.getFacultyObject(textBox1.Text.ToLower()); //Form5 form5 = new Form5(ref DDD, faculty.user); //string s = "Welcome " + faculty.firstName + " " + faculty.middleName + " " + faculty.lastName; //form5.ChangeLabelName(s); //form5.ShowDialog(); //flag = true; //form5.Refresh(); //break; } } foreach (DataRow row in DDD.AdminDB.Rows) { if (row.Field <string>(0) == textBox1.Text.ToLower() && row.Field <string>(1) == textBox2.Text) { Admin2 adm = DDD.getAdminObject(textBox1.Text.ToLower()); Form9 form9 = new Form9(ref DDD, adm.user); //Admin2 admin = DDD.getAdminObject(textBox1.Text.ToLower()); //Form2 form2 = new Form2(DDD, admin); //string s = "Welcome " + admin.firstName + " " + admin.middleName + " " + admin.lastName; //form2.ChangeLabelName(s); //form2.ShowDialog(); //flag = true; //break; string s = "Welcome " + adm.firstName + " " + adm.middleName + " " + adm.lastName; form9.ChangeLabelName(s); form9.ShowDialog(); flag = true; form9.Refresh(); break; } } foreach (DataRow row in DDD.StudentDB.Rows) { if (row.Field <string>(0) == textBox1.Text.ToLower() && row.Field <string>(1) == textBox2.Text) { Student2 student = DDD.getStudentObject(textBox1.Text.ToLower()); Form2 form2 = new Form2(ref DDD, student.user); //List<Course> studentsCourses = element.getRegisteredCourses(); //courseBox = new List<string>(); //if (studentsCourses == null) //{ // courseBox.Add("You are not registered for any courses yet!"); //} //else //{ // courseBox.Clear(); // foreach (Course c in studentsCourses) // { // courseBox.Add(c.ToString()); // } //} string s = "Welcome " + student.firstName + " " + student.middleName + " " + student.lastName; form2.ChangeLabelName(s); form2.ShowDialog(); flag = true; form2.Refresh(); break; } } if (flag == false) { MessageBox.Show("The Username or Password is Incorrect", "Error"); textBox2.Text = ""; } else { textBox1.Text = ""; textBox2.Text = ""; } }
static void main(string[] args) { /* * Inheritance - * allows us to define a class based on another class. This makes creating and maintaining an application easy. * The class whose properties are inherited by another class is called the Base class. The class which inherits the properties is called the Derived class. * for example, Base class Animal can be used to derive Cat and Dog classes. * The derived class inherits all the features from the base class and can have its own additional features * example line(12) */ Dog d = new Dog(); Console.WriteLine(d.Legs); //output is 4 d.Bark(); //output Woof //A base clase can have multiple derived classes. //for example, a Cat class can inherit from Animal //Inheritance allows the derived class to reuse the code in the base class without having to rewrite it. Also the derive class can be customized by //adding more members. In this manner, the derived class extends the functionality of the base class. /*A derived class inherits all the members of the base class, including its methods * example line(30) * */ Student s = new Student(); s.Speak(); // output Hi there //C# does not support multiple inheritance, so we cannot inherit from multiple classes, however, we can use interfaces to implement multiple inheritance /* * Protected - * This access modifier is very similar to private access modifier with one difference. * A Protected access modifier can be accessed in the derived classes. So a protected member is accessible only from derived classes * example line(43) */ Student2 s2 = new Student2("Iykeman"); s2.Speak(); /*Sealed - * A class can prevent other classes from inheritig it, or any of its members by using the sealed modifier. example * sealed class Animal { * //some code * } * class Dog : Animal { } // Error * The sealed keyword provides a level of protection to your class so that other classes cannot inherit from it */ /*Derived Class Constructor and Destructor * Contructors are called when objects of a class are created. With inheritance, the base class constructor and destructor are not inherited, so we should * define constructors for the derived classes. * However, he base class constructor and destructor are being invoked automatically when an object of the derived class is created or derleted * consider example line(61) * Note that the base class constructor is called first and the derived class constructor is called next. When the object is destroyed, the derived class * destructor is invoked and then the base class destructor is invoked. * we can think of this as the following: the derived class needs its base class in order to work which is why the base class constructor is called first */ Cow c = new Cow(); /* Polymorphism - * this word means having many forms, it occurs when there is a hierarchy of classes and they are related through inheritance from a common base class. * Polymorphism means that a call to a member method will cause a different implementation to be executed depending on the type of object that invokes the method. * This simplt means that a single method can have a number of different implementations. * -Consider having a program that alllows users to draw different shapes.Each shape is drawn differently and you do not know which shape the user will choose, * Here, polymorphism can be leveraged to invoke the appropriate Draw method of any derived class. Such methods must be declared using the virtual keyword in the base class * example below and line(85) * class shape { * public virtual void Draw() { * Console.WriteLine("Base Draw"); * } * } * The virtual keyword allows methods to be overridden in derived classes. * Virtual methods enable us to work with groups of related objects in a uniform way. * * we can derive different shape classes that define their own Draw methods using the override keyword. example line (93). * The virtual Draw method in the Shape base class can be overridden in the derived classes. In this case, Circle and Rectangle have their own Draw methods. * we can create separate Shape objects for each derived type and then call their Draw methods */ Shape cc = new Circle(); cc.Draw(); // output "Circle Draw" Shape rt = new Rectangle(); rt.Draw(); //output "Rectangle Draw" /*Polymorphism is useful in many cases, For example, we could create a game where we would have different player types with each player * having a separate behaviour for the Attack method. * In this case, Attack would be a virtual method of the base class Player and each derived class would override it. */ /*Abstract Classes - * In situation where it is not meaningful for the virtual method(polymorphism) to have a separate definition in base class, * we defined this method with the abstract keyword and specify that the derived classes must define their method on their own. * we cannot create objects of a class containing an abstract method, which is why the class itself should be abstract * lets use abstract method in the shape class as in polymorphism case, example line (113) * The draw method on line 133 is abstract and thus has no body, we do not even need a curly brackets, just end the statement with a semicolon. * The Shape class itself must be declared abstract because it contains an abstract method. Abstract method declarations are only permitted in abstract classess * Members marked as abstract or included in an abstract class must be implemented by classes that derive from the abstract class. * An abstract class can have multiple abstract members * Ab abstract class is intended to be a base class of other classess. It acts like a template for its derived classes. * we can now define other classes and define their own Draw() method. example line(117) */ Shape2 cc2 = new Circle2(); cc.Draw(); // output "Circle Draw" Shape2 rt2 = new Rectangle2(); rt.Draw(); //output "Rectangle Draw" /*Abstract class have the following features * An abstract class cannot be instantiated * An abstract class may contain abstract methods and accessors * A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors * Note that it is not possible to modify an abstract class with the sealed modifier because the two modifiers have opposite meanings. * The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited */ /* Interfaces - * An interface is a completely abstract class, which contains only abstract members. It is declared using the interface keyword, * Example - * public interface IShape * { * void Draw(); * } * All members of the interface are by default abstract, so no need to use the abstract keyword. * Also all members of an interface are always public, an no access modifiers can be applied to them. * It is common to use the capital letter I as the starting letter for an interface name. * Interfaces can contain properties, methods, etc but cannot contain fields (variables). * * When a class implements an interface, it must also implement, or define, all of its methods. * The term implementing an interface is used(opposed to the term "inheriting from") to describe the process of creating a class based on an * interface. The interface simply describes what a class should do. The class implementing the interface must define how to accomplish the behaviours. * The syntax to implement an interface is the same as that to derive a class. * example line (134) */ IShape ca = new CircleA(); ca.Draw(); //Output "Circle Draw" /*Note that the override keyword is not needed when you implement an interface * Why use interfaces rather than abstract classes? * A class can inherit from just one base class, but it can implement multiple interfaces! * Therefore, by using interfaces we can include behaviour from multiple sources in a class. * To implement multiple interfaces, use a comma separated list of interfaces when creating the class: Class A : IShape, IAnimal, etc */ /* * Nested Classes * C# supports nested classes, a nested class is a class that is a member of another class. example below & line (148) * class Car { * string name; * public Car(string nm) { * name = nm; * Motor m = new Motor(); * } * public class Motor { * // some code * } * } * * The Motor class is nested in the Car class and can be used similar to other members of the class. * A nested class acts as a member of the class, so it can have the same access modifiers as other members(public, private, protected). * * Just as in real life, objects can contain other objects. For example, a car, which has its own attributes(color, brand, etc) contains * a motor, which as a separate object, has its own attributes(volume, horsepower, etc). * Here, the Car class can have a nested Motor class as one of its members */ /*Namespaces * when we create a blank project, it has the following structure: * * using System; * using System.Collections.Generic; * using System.Linq; * using.System.Text; * using System.Threading.Tasks; * * namespace SoloLearn { * class Program { * static void Main(string[] args) { * } * } * } * * Note that our whole program is inside a namespace. So, what are namespaces? * Namespaces declare a scope that contains a set of related objects. You can use a namespace to organize code elements. * You can define your own namespaces and use them in your program. The using keyword states that the rpogram is using a given namespace. * for example, we are using the System namespace in our programs, which is where the class Console is defined. Without the using * statement, we would have to specify the namespace whereever it is used like below * System.Console.WriteLine("Hello world"); * * The .NET framework uses namespaces to organize its many classes. System is one example of a .NET framework namespaces. Declaring your * own namespaces can help you group your class and method names in larger programming projects * */ }