C Advanced Topics Methods Classes and Objects Soft
C# Advanced Topics Methods, Classes and Objects Soft. Uni Team Technical Trainers Software University http: //softuni. bg
Table of Contents § A very brief introduction to: 1. Methods § Utilizing a piece of code multiple times 2. Using Built-in. NET Classes 3. Defining Simple Classes § Making real life objects into code 2
Methods Defining and Invoking Methods
What are methods? § Methods are named pieces of code § Defined in the class body § Can be invoked multiple times § Can take parameters § Can return a value static void Print. Hyphens(int count) { Console. Write. Line( new string('-', count)); } static void Main() { for (int i = 1; i <= 10; i++) { Print. Hyphens(i); } } 4
How to define a method § Parts of a method: Return type Method name Method parameters public double Calc. Triangle. Area (double a, double h) { double area = a * h / 2; return area; } Return value 5
Void return type § The void return type means that: § Will not return a value § Will only execute some code without creating a new value public void print. Name(string first. Name, { Console. Write. Line("My full name is: first. Name, } string last. Name) {0} {1}", last. Name); 6
Methods with Parameters and Return Value static double Calc. Triangle. Area(double width, double height) { return width * height / 2; } static void Main() { Console. Write("Enter triangle width: "); double width = double. Parse(Console. Read. Line()); Console. Write("Enter triangle height: "); double height = double. Parse(Console. Read. Line()); Console. Write. Line(Calc. Triangle. Area(width, height)); } 7
Methods Live Demo
Using Built-in. NET Classes Math, Random, Console, etc.
Built-in Classes in. NET Framework §. NET Framework provides thousands of ready-to-use classes § Packaged into namespaces like System, System. Net, System. Collections, System. Linq, etc. § Using static. NET classes: Date. Time today = Date. Time. Now; double cosine = Math. Cos(Math. PI); § Using non-static. NET classes Random rnd = new Random(); int random. Number = rnd. Next(1, 99); 10
Built-in. NET Classes – Examples Date. Time today = Date. Time. Now; Console. Write. Line("Today is: " + today); Date. Time tomorrow = today. Add. Days(1); Console. Write. Line("Tomorrow is: " + tomorrow); double angle. Degrees = 60; double angle. Radians = angle. Degrees * Math. PI / 180; Console. Write. Line(Math. Cos(angle. Radians)); Random rnd = new Random(); Console. Write. Line(rnd. Next(1, 100)); Web. Client web. Client = new Web. Client(); web. Client. Download. File("http: //…", "file. pdf"); Process. Start("file. pdf"); 11
Using Built-in. NET Classes Live Demo
Defining Simple Classes Using Classes to Hold a Set of Fields
Classes in C# § Classes in C# combine a set of named fields / properties § Defining a class Point holding X and Y coordinates: class Point { public int X { get; set; } public int Y { get; set; } } § Creating class instances (objects): Point start end = = new Point() { { X X = = 3, Y = 4 }; -1, Y = 5 }; 14
Arrays of Objects § We can create arrays and lists of objects: Point[] { new new }; line = new Point[] Point() { { X X = = -2, Y = 1 }, 1, Y = 3 }, 4, Y = 2 }, 3, Y = -2 }, for (int i = 0; i < line. Length; i++) { Console. Write. Line("Point(" + line[i]. X + ", " + line[i]. Y + ")"); } 15
Defining and Using Classes – Example class Person { public string First. Name { get; set; } public string Last. Name { get; set; } public int Age { get; set; } } … Person[] people = new Person[] { new Person() { First. Name = "Larry", Last. Name = "Page", new Person() { First. Name = "Steve", Last. Name = "Jobs", new Person() { First. Name = "Bill", Last. Name = "Gates", }; Age Age = = = 40}, 56}, 58}, 16
Defining and Using Classes – Example (2) Console. Write. Line("Young people: "); foreach (var p in people) { if (p. Age < 50) { Console. Write. Line("{0} (age: {1})", p. Last. Name, p. Age); } } var young. People = people. Where(p => p. Age < 50 ); foreach (var p in young. People) { Console. Write. Line("{0} (age: {1})", p. Last. Name, p. Age ); } 17
Defining and Using Simple Classes Live Demo
Summary § Methods are reusable named code blocks § Can return different type of data §. NET Framework provides a rich class library § Math, Random, Date. Time, System. Linq § Classes combine a set of fields into a single structure § Objects are instances of classes 19
C# Advanced Topics ? s n o i t s e u Q ? ? ? http: //softuni. bg/courses/csharp-basics
License § This course (slides, examples, demos, videos, homework, etc. ) is licensed under the "Creative Commons Attribution. Non. Commercial-Share. Alike 4. 0 International" license § Attribution: this work may contain portions from § "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license 21
Free Trainings @ Software University § Software University Foundation – softuni. org § Software University – High-Quality Education, Profession and Job for Software Developers § softuni. bg § Software University @ Facebook § facebook. com/Software. University § Software University @ You. Tube § youtube. com/Software. University § Software University Forums – forum. softuni. bg
- Slides: 22