Lesson 1: Property,
List and Enum
Chapter 3: C# overview
1. List, enum, properties and indexers
2. Struct and in, out, ref
3. Sugar
4. Generics (other common data structures and custom generic types,
constraints)
5. Multidimensional arrays, jagged arrays, dictionary
6. Delegates and events
7. Extension methods
8. LINQ 1 (basics)
9. LINQ 2 (advanced cases and query syntax)
10. Attributes
11. Json and xml, how to use NuGet
12. Operators overloading
13. Overview of useful, but less often used features: tuples, deconstruction,
expressions, reflection, preprocessor, unsafe code...
Agenda
● What is a generic type?
● What is a list?
● List vs Array
● Property vs
Getters/Setters
● 3 different ways of
defining properties
● Enum
● Enum vs const
The cool syntactic C# sugar
starts here!
List
What if we could...
● Add or remove elements
to an array
● Remove element at any
index or by any value
● Have a whole bunch of
utility methods for
something that holds
many elements
● We can!
● The answer is List!
Very similar to array, except no size!
● List is a data structure
designed for the same
purpose as array- for
storing something many
● Unlike array, List does all
the tedious work for
resizing under the hood
● Therefore you don’t have
to know how big it is in
order to use it
List is a class, but not just any class- a generic
class!
● Arrays across languages
tend to have the same
syntax ([])
● However a some special-
syntax based thing used
for storing the same of
many
● It just means that the
generic type (<T>) can be
anything
Size doesn’t matter...
● List does has a size property when initializing it
● But it’s more of an optimization thing rather than
something you should put too much attention for
Property
What is a property?
● Property is a method
which semantically looks
like a field (from outside)
● Property is either a
setter or getter methods
(or both)
● In c# we don’t use
GetFoo, SetFoo, we use
properties
● If not specified (for
get/set), access modifier
of a property comes
Under the hood
● Properties are just
special getter and setter
methods with a backing
data field
● You don’t need to
declare neither names or
types of input- it’s all
known based on
property declaration
Custom getter /setter
● Getter and setter again,
are methods
● Implementing each
requires opening a scope
● The value that you set in
a setter comes through a
keyword- value
3 different ways to declare properties
1. Expression-bodied
properties
2. Auto properties
3. Custom properties
Why properties and not fields or methods?
● It’s more readable to
have properties over
get/set methods
● We don’t expose class
data (fields), therefore to
have some
encapsulation, we use
setter/getter methods or
properties
Indexer
● A special property
● A handy way to access
something of a class
using some key/filter
Enum
A bunch of tags
● Enum is just a collection
of tags
● Is a type
● Similar to constant
Indexing of enum
● Starts from 0
● Can be custom assigned
● If no value specified,
next enum will be +1 to
previous
● Can be casted to int
● ToString() will return the
enum value (not int)
Enum vs Constant
● Enum is very similar to
constant as it is
predefined set of values
which won’t change
● However enum itself is a
type
● And by being a type it
will not accept values
outside of it (so you get
validation compared to
const)

Chapter 3. Lesson 1_ Properties and lists.pptx

  • 1.
  • 2.
    Chapter 3: C#overview 1. List, enum, properties and indexers 2. Struct and in, out, ref 3. Sugar 4. Generics (other common data structures and custom generic types, constraints) 5. Multidimensional arrays, jagged arrays, dictionary 6. Delegates and events 7. Extension methods 8. LINQ 1 (basics) 9. LINQ 2 (advanced cases and query syntax) 10. Attributes 11. Json and xml, how to use NuGet 12. Operators overloading 13. Overview of useful, but less often used features: tuples, deconstruction, expressions, reflection, preprocessor, unsafe code...
  • 3.
    Agenda ● What isa generic type? ● What is a list? ● List vs Array ● Property vs Getters/Setters ● 3 different ways of defining properties ● Enum ● Enum vs const The cool syntactic C# sugar starts here!
  • 4.
  • 5.
    What if wecould... ● Add or remove elements to an array ● Remove element at any index or by any value ● Have a whole bunch of utility methods for something that holds many elements ● We can! ● The answer is List!
  • 6.
    Very similar toarray, except no size! ● List is a data structure designed for the same purpose as array- for storing something many ● Unlike array, List does all the tedious work for resizing under the hood ● Therefore you don’t have to know how big it is in order to use it
  • 7.
    List is aclass, but not just any class- a generic class! ● Arrays across languages tend to have the same syntax ([]) ● However a some special- syntax based thing used for storing the same of many ● It just means that the generic type (<T>) can be anything
  • 8.
    Size doesn’t matter... ●List does has a size property when initializing it ● But it’s more of an optimization thing rather than something you should put too much attention for
  • 9.
  • 10.
    What is aproperty? ● Property is a method which semantically looks like a field (from outside) ● Property is either a setter or getter methods (or both) ● In c# we don’t use GetFoo, SetFoo, we use properties ● If not specified (for get/set), access modifier of a property comes
  • 11.
    Under the hood ●Properties are just special getter and setter methods with a backing data field ● You don’t need to declare neither names or types of input- it’s all known based on property declaration
  • 12.
    Custom getter /setter ●Getter and setter again, are methods ● Implementing each requires opening a scope ● The value that you set in a setter comes through a keyword- value
  • 13.
    3 different waysto declare properties 1. Expression-bodied properties 2. Auto properties 3. Custom properties
  • 14.
    Why properties andnot fields or methods? ● It’s more readable to have properties over get/set methods ● We don’t expose class data (fields), therefore to have some encapsulation, we use setter/getter methods or properties
  • 15.
    Indexer ● A specialproperty ● A handy way to access something of a class using some key/filter
  • 16.
  • 17.
    A bunch oftags ● Enum is just a collection of tags ● Is a type ● Similar to constant
  • 18.
    Indexing of enum ●Starts from 0 ● Can be custom assigned ● If no value specified, next enum will be +1 to previous ● Can be casted to int ● ToString() will return the enum value (not int)
  • 19.
    Enum vs Constant ●Enum is very similar to constant as it is predefined set of values which won’t change ● However enum itself is a type ● And by being a type it will not accept values outside of it (so you get validation compared to const)