MonthFebruary 2016

Code Snippets

Code snippets are convenient way to save time wile writing code. By typing just a few easy to remember keywords you will save a lot of time and implement desired outcome successfully.

By typing following keywords and pressing tab following outcomes will appear. Beware, keywords are case sensitive.

 

class + Tab will create class

 

enum + Tab will create enum

 

 

ctor + Tab will create constructor

 

 

propfull + Tab + string + field+ Tab + Property will create property

 

 

prop + Tab + int + Tab + AutoImpelemntedPropertyName will create auto implemented property

 

 

svm + Tab (Useful for your Console program, but remember only one is Main method is allowed)

 

 

foreach + Tab + dataTypeName + item + collectionName will create foreach loop

 

 

 

try + Tab will create try-catch block

 

 

tryf + Tab will create try-catch block

 

 

 

Constructor

Constructors are a public methods with the same name as a class were they resides. Constructors are always created with a class, weather we write it ourselves or if class creates it for us(although it will not be visible ). Role of constructors is to initialize fields when we instantiate class. It is possible to create multiple constructors. Adding „: this()“ after parameters of constructor with parameters will execute default constructor before constructors with parameters. To easily create constructors type ctor keyword an new one will be generated for you.

 

If you have ever initialized class you have consumed constructors in one way or another. Example of consuming constructors would be in previous example where we have instantiated ClassWithConstructor class with string “Mike” and integer 1.

 

Is it possible to forbid class to initialize constructor?

Yes it is, just create empty one and set private access modifier.

 

What about other access modifiers? Can I use them all?

Public, private, protected, internal, or protected internal access modifiers are allowed to be used with constructors.

 

How can i initialize base class constructors ?

Simply type

after constructors parameters . Remember, you don’t have to supply parameters for base class constructor  because base constructor will always be called before derived class constructor, weather we call it or not.

Explicit Interface member implementations

Explicit Interface are implemented explicitly if for example two or more of our interfaces implement same method/properties/etc… names and different implementation. Other usages can be to implement strong typing and cleaner code.

 

 

Implicit Interface member implementations

Example will show how to create new interface called ICarSpecifications which will hold a property and a method. ICarSpecifications interface will be implemented inside Car class and its implementation will be inside region InterfaceImplementation(note: you can manually create implementation OR when you type ICarSpecifications pres keys CTRL + . and click Implement missing members. Region will not be implemented automatically).

 

Tomorrow we will continue with explicit interfaces implementations.

Interfaces

Interfaces are a type of contract between interface and class or struct. Interface is defined by interface keyword and the name of interface should usually start with capital letter I to make it more recognizable.

 

When we say Interface is a contract, what do you really mean?

Every class or struct that uses some interface must implement all of its functionalities.
Why would you use Interface at all?

There are multiple reasons actually:

Firstly, a class or classes that use Interface must use all of Interfaces functionalities. Secondly, names of all properties, methods… must be same in all classes as they are in that specific Interface. Furthermore Interfaces are obligatory, imagine situation where you must in a few classes use 3 methods Get, Insert and Delete, when you use interface with your classes there is no possibility of forgetting to implement either of these methods. Lastly, because C# does not allow us to inherit multiple classes but allows us to implement multiple Interfaces, we can create multiple interfaces where each interface inherits a class and use these interface as workaround.

 

What can we define inside Interface?

Events

Indexers

Methods (but without implementation)

Properties

 

What are Interface limitations?

Interfaces cannot define:

Constants

Constructors

Fields

Operators

We cannot make interface or its members static

 

What about access modifiers?

By default all interface members are public and we cannot change it.

 

Can we instantiate Interface? Yes we can, limitation is that you must instantiate them using a class or struct that implements that interface.

 

Is there a limit to how many Interfaces can we implement at the same time? Yes and no. Although language specification state that you can implement as many Interfaces as you like, there are few technical limitations: heap size and metadata tables size, but worry not, you will never exceed that limit.

 

Please, drop by tomorrow to see a few Interface examples .

Explicit Interface member implementations

Implicit Interface member implementations

 

 

 

 

 

Inheritance

Inheritance

What exactly is Inheritance? MSDN explanation says : Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

So what can we inherit? For example we have Car and Motorcycle classes, and as we know cars and motorcycles have a few things in common such as engine and color. We could create a base class named Vehicle that has those properties and make Car and Motorcycle classes which will inherit Vehicle class.

 

Can i inherit from multiple classes? No, but you can Inherit multiple Interfaces that inherit one class.

 

I don’t wish for my class or method to be directly instantiated. What can I do about that problem? You can make those classes or methods abstract. Beware, when you make your class abstract one, none of your class members have to be marked as abstract, but if at least one of your members is marked as abstract, entire class must be marked as abstract.

 

Can derived class access private fields of base class? No, it can not. You can only access internal, public, protected, and protected internal members of a base class.

 

Is it possible to prevent our class from becoming other classes base class? Yes, mark that class as sealed and other classes cannot inherit from this class.

Properties

It is good practice to keep our fields private but so far we had to create at least two methods to access or change their values.

 

Why not star using properties? You consume properties as fields and write them (almost) as methods.

Simple property example:

 

But why stop there we can add more functionality to our properties, like set our string name to Upercase, return default value or check if integer is a positive number.

 

A property that has both accessors is read-write property. Properties with only get accessor are called read-only properties, properties with only get accessor are called write-only properties.

 

When you only need basic property without any additional logic, you could create auto-implemented property without filed (actually compiler creates one for you).

Simple isn’t it 🙂

Encapsulation example

In our encapsulation example we will create 3 classes:

Color

Car

And Manufacturer

 

In color Class there will be 2 private read-only fields containing colors and two public methods to access them. Fields will be private read-only because we do not want someone to access or change their values, and methods will be public because we want to access values from our fields.

 

Car class will also contain 2 private fields but with car model names and two public methods which can access our fields, but we will also create two public static integers whose values we will access and alter in Manufacturer class.

 

Manufacturer class will access methods from Color and Car classes and alter one of static integers from Car class.

Encapsulation example:

 

 

 

Encapsulation

Encapsulation is one of main four object-oriented programming concepts. Encapsulation is part of OOP where we decide on which depth level another user (programmer) can can dig in our code. Encapsulation is implemented by using access modifiers.

List of access modifiers:

  • Public
  • Private
  • Protected
  • Internal
  • Protected internal

Public access modifier allows a class to expose its variables and methods to other classes/methods/objects/etc.. inside and and outside of it’s original class even outside of original assembly.

Private access modifier is default access modifier, meaning if we do not specify access modifier explicitly private is set as default one. Only methods of the same class can access its private members, meaning an instance of a class cannot access its private members L.

Protected and Protected internal access modifier are mostly used in implementing inheritance (one of the pillars of OOP). They allow child class to access variables and methods of base class. These access modifier will be explained later in Inheritance chapter.

Internal access modifier, same as public but is limited only to its root assembly. Internal is default access modifier of classes.

© 2019 LearnC#WithDarko

Theme by Anders NorenUp ↑