Let’s imagine that you have a class with two methods:
Now when we want to create an instance of this class and call one of those methods, it executes as expected:
Now suppose that we have an interface called IVendingMachine that has two methods:
We want our class to implements this interface:
Our class satisfied the interface because it has the methods with the same names. So Visual Studio doesn’t give you a compiler error. Now let’s imagine that we
also want to add interface’s methods to this class, So, in this case, we must explicitly prefix the methods with IVendingMachine.:
Note that Visual Studio can help you to implement interface explicitly by pressing Ctrl + . on the name of the interface and select Implement interface explicitly:
Now, what happens when we call the InsertCoin and Buy methods? In this case, we should consider two different situations when we create the object:
If we want the methods of VendingMachine we should create the object with concrete type:
If we want the methods of IVendingMachine we should create the object with interface variable:
Lately I have been studying some topics in C# in order to improve myself. I think Delegate is one of those topics which is kinda difficult for beginners to initiate with. So, in order to make it clear how it works, I’m going to make some explanations in this post.
What are Delegates?
Delegate was introduced in C# 1. Think of it as a simply being a placeholder for functions that’ll be called at some point in time. Assume you want to declare a variable that is a reference to a specific method. In this case, the variable encapsulates some executable code and you can invoke the variable just like an actual method. In doing so, we need to create a delegate
Defining a Delegate
Delegates are created using delegate keyword along with the function signature:
As you can see we are defining a delegate called Sum that is compatible with any method with an int return type with two parameters. Actually we defined a type that we can use to create variables and point those variables to methods that have the same signature and same return type.
The type defenition for a delegate describes the method that we want to call:
Now we can assign a method to the delegate variable. This assignment creates a delegate instance:
Now we can invoke the delegate instance in the same way as a method:
Advantages of Delegate
Delegates are type safe, It means that the C# compiler checks the function signatures of a Delegate when you use them at runtime.
Can be used to define callback functions.
Can be dynamically switched at runtime.
Real World example
Suppose that you want to implement something like a Repeater control in your ASP.NET MVC application, As you probably know a Repeater is(was) server side databound control in ASP.NET WebForm for displaying information, It had a lot of flexability becuase you had complete control over the your markup.
As you can see we emulated the ItemTemplate and AlternatingItemTemplate using a delegate. In this case user can pass the data and the data will be rendered inside the view. In this case the delegates act like callback.
The ItemTemplate delegate in our example can be replaced with Func delegate:
Now we can use our helper inside any view:
If you run the application you can see the result:
Today I was talking with a friend of mine about thinking, The topic is interesting and I wanted to share it with you because as a programmer we are always thinking and trying to find solutions. Actually it’s a good habit when you want to learn something new, instead of taking action and delve into the details, you should think about that and break it down into several pieces then construct it as a big picture. Once you get familiar to it you can start trying to figure out each individual piece, By doing so you will become great in your field.
On the other hand, thinking isn’t the only thing for being an expert in programming. Suppose that you are given a challenging feature to implement in a team you’re working with, you might spend days and days trying to implement this feature and each time you fail and stuck in it then you try to find a solution on the net or asking questions on Stackoverflow. I’m not saying you shouldn’t google about your problem, What I’m saying is that you should understand the problem, This is where a Thinking out of the box’s metaphor comes into play.
This is very helpful because when you ran into a problem you should first think about it in a new or a different prespective, For example you can put yourself in end user’s place and try to figure it out. Once you understand it, you can start coding the feature. By doing so you can help yourself and believe me it works like a charm and you don’t need to post off-topic questions every single day on Stackoverflow anymore, Instead when you run into a serious problem, you can post your question and get extra votes :)
So thinking out of the box helps us to think like a programmer, It also helps you to approach problems in new ways.
How I got into AngularJS
Angular has lots of great features from testability and two-way binding, to more vague concepts. For me the best thing about Angular is that it has a flat learning curve So you can get the most of it. All being said here are the some of my favorite features and benefits of Angular:
Code Reduction: Angular reduces the amount of code that developers write.
Two Way Binding
Beautiful syntax and higher level APIs
Whereas with Angular you can simple achieve same goal this way:
As you can see the Angular code is less complex.
Angular was designed with testing in mind. So you can test any components of your application easily through both unit testing and end to end testing. For unit testing you can use a component called ngMock and for e2e testing you can use Protractor. It is worthwhile to mention that for both cases you can run your tests using a tool called Karma.
Although Angular is a great framework but it has some problems that you can run into trouble with it. Here are some of them:
For dealing with this issue we can use server side rendering or use a prerenderer,
One thing that causes performance problem in Angular is having too many bindings. If performance is important you can consider using a different rendering engine.
External Events and Digest Cycle
Even though Angular 2 is coming, But Angular 1 is still a thing and I think Angular 1 will be supported for a long time to come.
Assume that we want to pull down some data from the server, Then the data is going to be processed through an array loop, And we are going to build some DOM elements:
Also the action method is like this:
The buffer method works like a charm but using setTimeout is considered to be a bad practice. So that’s where the Web Workers come into play.
So let’s rewrite our example with Web Workers:
As you can see I instantiated a worker, Then I setup an event listener for message, So when data comes in from the AJAX call, we will inject the result into html of a container called result.
Here you see the worker:
Inside the fetch content we used typical AJAX call because in Web Workers you can not use a third-party library it means that there’s no access to non-thread safe components, So you have to use a native XMLHttpRequest in your worker. Now if you run the code you will see that now that’s pretty fast even quite a bit faster than before.
In many programming languages a class is a standard unit of work, which means that you normally create all of your code inside classes and these classes contain data, behaviour, events and those sort of things that you are going to use in developing your solutions. for example in C# we can create a class this way:
So what this function does is use the this keyword to define new property that represents a person object in this case. And what’s interesting here this is what’s called a constructor syntax. So this function when called will return an object. By doing this, we can create a new person:
In this case, we can see that it is using the new keyword. this keyword is used to find a function with a name, after the new, return an object that contained the shape that is defined in the function. Once we have an instance of that, we can get any of the properties that are specified in the constructor syntax function:
ES6 introduces language support for classes with class keyword. So the ES6 equivalent of the Person function would be the following:
So we have essentially recreated the same Person data type but with much better syntax. behind the scene the runtime is doing the exactly what we used to do. But now the syntax is clean and expressive. As you can see there is constructor in the class definition. the constructor is one of the class members that we can list in the body of a class, just like the other members it is a function, it is a function that automatically invoked when we say new and use the class name as a function call:
###Getters and Setters
In a class we can also use getter and setter, they are created using get and set keyword. get and set allows us to run code on the reading or writing of a property:
ES6 also gives us an easy syntax to specify and inheritance for relationship. Which is a way we say a class inherits from another class. Now assume that we need another class for modling an Employee, since every employee is a person, maybe we will have the Employee inherit from a Person. In ES6 inheritance is specified using extends keyword:
The only thing is that a lot of features in ES2015 are not supported in the browsers. For example classes are not supported by any browser, now there might be a nightly built of Firefox that supports classes. So instead we have to use a transpiler and there are many transpilers such as TypeScript, Bable and variety of others. I recommend you to use Bable because it has the best support for all of ES2015 features.
As you know in C# we have something called Object and collection initializer. For example we can use object initializer to create name and value pairs for different properties of the object, in this case car object:
As you can see we used object initializer for those embedded objects too.
Although object initializers can be used for anonymous types. So here’s the same example which we build with anonymous object:
As you can see we use key/value pairs. Actually it’s a little different from C# for instance we used colons (:) instead of equals (=) but generally the syntaxes are similar. In some case you may see the name is embedded in quotes, it’s optional but if you’re using any reserved characters like space, in this case, you need to surround the name with quotes (like towing capacity).
You can also use a bracket syntax, that’s why objects are sometimes called associative arrays:
This way we can retrieve or set the property values. There are other ways for accessing and retrieving property value that are as follows:
We can also get embedded objects that are the properties of those dynamic objects as well and retrive properties from them:
ExpandoObject Represents an object whose members can be dynamically added and removed at run time.
For example in this code we are creating a new object and marking it as dynamic so that we can add members to this ExpandoObject as we want:
We can still add another property or function on-the-fly: