Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Thursday, 4 August 2016

Delegates and Events in C Sharp

In this article, I will brief about delegates and events. C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.

Declaring, Instantiating and Multicasting of delegates
Below screen shot explains, void vs return type delegates
Events and delegates work hand-in-hand to provide a program’s functionality. Below code shows logging feature using event delegate.

OOPs Concepts in Csharp Dotnet

In the article, I would brief about C#.net OOPs concepts. Lets create a new console application. Open  VS 2012 >New Projects > Visual C#  >  Windows> Console Application. And then add a class called BlogPage, this class has public property functions , private variable and public method and 1 public virtual method as shown. 
In our main method create an object of BlogPage and set members as below and run the application.
Let about one more class file and define two WordPressBlog and GoogleBlog. These classes inherit BlogPage and overrides the virtual BlogLayout method according to its own implementation. Let create object of WordPressBlog and GoogleBlog classes and run the application. In output console, BlogLayout outputs are shown are from the derviced class implementation.
Lets see a kind of real time, implementation using this example. Instead of base class feature, we would define BlogPage as an abstract class which can only be inherited and abstract method should be implemented in derived class. 
In the main method, based on users input, we would create an object of derived class as shown below.

Basics in Csharp DotNet | Crash Course

In the article, I would brief about C#.net basic concepts. This would help to understand the object oriented concepts and help in the developing of services.
Let create a new console application. Open  VS 2012 >New Projects > Visual C#  >  Windows> Console Application.
As you see, we have program class containing a static main method. This is entry point to the application. Let write Console.WriteLine and provide a meaningful sentence as shown and run the program using F5 and you can see the output in the console window.
Understanding simple data types, variables are best thought of as containers or buckets into which you put data.  Below code shows example of string, int and Datetime 
Below example shows, how to take inputs from user and display it in console output window
Functions are the primary method of code organization and reuse in C#.Functions are groups of code that have a name, and can be called using parentheses. Below example shows function/method with void and return type.
Control flow is where the rubber really meets the road in programming. Without it, a program is simply a list of statements that are sequentially executed. With control flow, you can execute certain code blocks conditionally and/or repeatedly, these basic building blocks can be combined to create surprisingly sophisticated programs!
Conditional Statements: The “if”, “elseif,” and “else” statements are the most commonly used control flow statements.
When you define a class, you define a blueprint for a data type. This does not actually define any data, but it does define what the class name means. That is, what an object of the class consists of and what operations can be performed on that object. Objects are instances of a class. The methods and variables that constitute a class are called members of the class.

Here is helper class, consists of private variables, a public method, class constructor and destructor as shown. 

A class constructor is a special member function of a class that is executed whenever we create new objects of that class.A constructor has exactly the same name as that of class and it does not have any return type. A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope. A destructor has exactly the same name as that of the class with a prefixed tilde (~) and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing memory resources before exiting the program. Destructors cannot be inherited or overloaded.