Friday, 4 January 2019

Polymorphism

Polymorphism (OOPS Principal): Poly + Morphism (Poly means Many and Morphism means change or faces/ behaviors)
Definition: Object changes behavior according to the condition. (one name having multiple forms)

There are two types:
1) Static or compile-time Polymorphism (eg: Method overloading, operator overloading)

2) Dynamic or run-time Polymorphism (eg: overriding using virtual and override keywords with inheritance)

Static Polymorphism: or Method Overloading
Example :  (at compile-time, the compiler decides which method to call)

class Program
{
  static void Main(string[] args)
  {
     Mathematics mathObj = new Mathematics();
     mathObj.Add(1,2);
     mathObj.Add(1,2,3);
     Console.ReadLine();
  }
}

class Mathematics
{
  public void Add(int value1, int value2)
  {
     Int result = value1 + value2;
     Console.Writeline(result);
  }

public void Add(int value1, int value2, int value3)
  {
     Int result = value1 + value2 + value3;
     Console.WriteLine(result);
  }
}

Dynamic Polymorphism: (Inheritance, Virtual and Overriding)

Example: (at run-time, the compiler will decide which method to call)

class Program
{
  static void Main(string[] args)
  {
// if we have one parent class with so many child classes, at runtime parent // class instance can call or point to any of the child class override mathod.
     
BaseClass obj = new ChildClass();
// parent class can now point to the child class
     obj.Test();
  }
}

class BaseClass
{
  public virtual void Test()
  {
     Console.Writeline("BaseClass Method");
  }
}

class ChildClass : BaseClass
{
 public override void Test()
  {
     Console.Writeline("ChildClass Method");
  }
}


Reference: csharpcorner & Questpond

Thursday, 9 November 2017

?? in C# (double question mark operator in C#) Or null coalescing operator

null coalescing operator


The operator "??", yes two question marks. called as  "null-coalescing" or just the double question mark operator.

According to the Microsoft, help page: "The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand."

eg : var result = answer1 ?? answer2;
            can also be written as below
var result = answer1 != null ? answer1 : answer2;

if answer1 is not null =>
     result = answer1

if answer1 is null =>
   result = answer2

?? will be better uasable when we have to check for multiple null values for a result

string result = answer1 ?? answer2 ?? answer3 ?? answer4;
For more information please check
https://stackoverflow.com/questions/446835/what-do-two-question-marks-together-mean-in-c

Thursday, 13 July 2017

Process with an ID ???? is not running in visual studio 2015

Possible Solution 1: 

  • Close VS.
  • Navigate to the folder of the solution and delete the hidden .vs folder.
  • Restart VS.
  • Hit F5 and IIS Express should load as normal, allowing you to debug.
Possible Solution 2: 
  • Close all instances of Visual Studio
  • Rename the IISExpress folder (in my PC is in C:\Users\jmelosegui\Documents)
  • Add the _CSRUN_DISABLE_WORKAROUNDS Environment System variable with the value of 1. Here is how.
  • Start Visualstudio in administrator mode
Possible Solution 3:
  • open vs as an administrator
  • right click project and click on unload project
  • again right click project and click on open edit.... csproj
  • find the code below and delete it $
    <DevelopmentServerPort>63366</DevelopmentServerPort>
    <DevelopmentServerVPath>/</DevelopmentServerVPath>
    <IISUrl>http://localhost:63366/</IISUrl>
    
  • save and close the file .csproj
  • right click project and reload it
Source : https://stackoverflow.com/questions/26424902/process-with-an-id-is-not-running-in-visual-studio-professional-2013-update


Thursday, 13 March 2014

C# Structure

Why C# structures cannot have explicit parameterless constructor?

Answer: structures are value types. value types in C# contains default values. 
example: integer which by default value is numerical zero.

Remember : Structures can contain parametrized constructor.

more on C# structure

  • To define a structure we will use C# keyword called "struct"
  • structure is a value type. (its a protocol)
  • when we instantiated structure , it will be allocated in stack memory.(because all struct is a value type and all value types are will be allocated a stack memory)
  • structure cannot contain destructor.(only class can have destructor) WHY? 

Answer: struct being value type, are not subject to garbage collection. for more click here
  • we cannot use abstract and sealed keyword while defining a structure. C# Struct does not support inheritance.
  • we cannot use protected and protected internal access modifier for structures or structure members because it will not support inheritance.
  • To create an object for structure we don't required to use new operator.(When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.)


C#.NET Interface

C Sharp - Interface

  • By default Interface is Internal
  • All interface members are implicitly public and abstract so the members of the interface never specify access modifier.
  • Interfaces do not have an implementation of members(protocol). so interfaces cannot have data fields and also interfaces do not have constructors.
  • It is illegal to allocate Interface types.
  • Interfaces has read/write properties
  • Interfaces can contain event and indexer definitions.




Monday, 24 February 2014

ASP.NET Web API


What is ASP.NET Web API?


ASP.NET Web API is a framework for building web APIs on top of the .NET Framework.

What Model means in ASP.NET Web API?


A model is an object that represents the data in your application. ASP.NET Web API can automatically serialize your model to JSON, XML, or some other format, and then write the serialized data into the body of the HTTP response message. As long as a client can read the serialization format, it can deserialize the object. Most clients can parse either XML or JSON. Moreover, the client can indicate which format it wants by setting the Accept header in the HTTP request message.

What Controller means in ASP.NET Web API?


In Web API, a controller is an object that handles HTTP requests. We'll add a controller that can return either a list of products or a single product specified by ID.


Source

Monday, 25 March 2013

Constructors in C Sharp


  • Constructors are the core concept of Object Oriented Language, So the compiler generates a default constructor with default field values at compile time, only if there is no custom constructor was created.
  • If the class or struct has a custom constructor , the compiler silently removes the default constructor.
  • When a class or struct is created , its constructor is called(Invoked when a class is instantiated and objects memory location is created).
  • Constructors can be overloaded and they don't return values so no return type is used including void.
  • we can call one constructor from another using 'this' keyword.
  • Car(int modelNumber) :  this() {  }
  • We can also use 'base' keyword if the class is inherited and wants the parent class constructor values to be loaded to child class.
  • Constructors should be public so that values will be assigned to current state(fields).
  • If private, we cannot instantiate the class(because the constructor will not be available or not accessible outside of that class.)
  • we can also create static constructor but only static fields can be assigned.
  • If struct , parameter constructors are not allowed.
  • It is legal to create custom constructor(struct type) with no parameters. As struct type is value type 'new' keyword to allocate memory object is optional.
  • Remember: Static constructors will be executed at random time but before the class is instantiated.