Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
1.2 Basics: object orientation
//1.2 Basics: object orientation using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //C# is object oriented. Object is an instance of a class. A class is user defined type. //When facing a problem it's convinient to introduce some kind of entities that represent problem's domain entities. //These entities are described by classes. //So below we've described a Car entity and a Wheel one. //A class, or user defined type, can have data (like list of wheels) and methods that are supposed to process this data (like HowManyWheels method). //Classes are nice way of encapsulating data and logic. //Objects are instances of classes, they are created with keyword new and are kept in memory Car car = new Car(); Car another_car = new Car(); //Unlike primitive types, user defined types are passed by reference //I.e. when we pass object to a function, we pass its adress and not a copy Console.WriteLine(car.HowManyWheels()); MutateCar(car); Console.WriteLine(car.HowManyWheels()); //Created instance of types, or objects, will stay in memory for as long as someone keeps a reference to it //When object is not referenced anymore it will at some point be destroyed automatically } public static void MutateCar(Car car) { car.wheels = new List<Wheel>(); } } public class Car { public List<Wheel> wheels = new List<Wheel> {new Wheel() {WheelType = 1}, new Wheel() {WheelType = 1},}; //this is just initialization of a list wheels with two wheels public int HowManyWheels() { return wheels.Count(); } } public class Wheel { public int WheelType { get; set; } } }
run
|
edit
|
history
|
help
0
Bubble Sort
asxsdf
перегружаемый оператор
Dungeon Game
seoituerwoi
Func Delegate
Test
TesteSwitch
2.1 Basic types: Lists and Linq beauty
test coeur