Saturday 10 December 2016

Exception Handling

Exception Handling (try- catch- finally)


What happens if a finally block throws an exception?


Three things will be happen

ONE # That exception propagates out and up, and it should be handled at a higher  level. If it is not handled at the higher level, the application crashes.

TWO # The "finally" block execution stops at the point where the exception thrown.

Three # 
a) if the "finally" block is being executed after an exception has occurred in the try block,
b)and if that exception is not handled 
c) and if the finally block throws an exception
Then the original exception occurs in the try block is lost.



Will finally run if i put return in catch block?

Yes.The finally section is guaranteed to execute whatever happens including exceptions or return statement.


Does finally get executed if the code throws an error ?

Finally is always executed.


Is it mandatory for a piece of code to have catch or finally when try block is there ?

Yes. If a try block is there then either catch or finally has to be there or else a compiler error is generated.


Can finally bock have return statement ?

No,a compile time error is generated. Thats bcz finally contains the clean up code.


Can there be many catch statements for a single try block ?

Yes.But the rule is most specific exceptions should be caught first.


Will the foll code compile ?

           try 
            {
                throw new SystemException();
            }            
            catch(Exception e)
            {
            }
            catch(ArgumentException e)
            {
            }
No.It gives the foll error
A previous catch clause already catches all exceptions of this or of a super type ('System.Exception')





Thank you for taking your valuable time look at my blog - Sadiq Ali Syed



Friday 9 December 2016

Sealed Class


Sealed Class


Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as a sealed class, the class cannot be inherited. 

In C#, the sealed modifier is used to define a class as sealed. If a class is derived from a sealed class then the compiler throws an error. 

If you have ever noticed, structs are sealed. You cannot derive a class from a struct.  

The following class definition defines a sealed class in C#: 


        // Sealed class
       sealed class SealedClass
       {

       } 



In the following code, I create a sealed class SealedClass and use it from Class1. If you run this code then it will work fine. But if you try to derive a class from the SealedClass, you will get an error. 

        using System;
        class Class1
        {
   
        }
        // Sealed class
        sealed class SealedClass
        {
    
        }  

Sealed Methods and Properties 

You can also use the sealed modifier on a method or a property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent other developers that are using your classes from overriding specific virtual methods and properties. 

class X
{
  protected virtual void F() { Console.WriteLine("X.F"); }
  protected virtual void F2() { Console.WriteLine("X.F2"); }
}

class Y : X
{
  sealed protected override void F() { Console.WriteLine("Y.F"); }
protected override void F2() { Console.WriteLine("X.F3"); }
}

class Z : Y
{
  // Attempting to override F causes compiler error CS0239. 
  // protected override void F() { Console.WriteLine("C.F"); }
// Overriding F2 is allowed. 
  protected override void F2() { Console.WriteLine("Z.F2"); }
}

Why Sealed Classes?
 

We just saw how to create and use a sealed class. The main purpose of a sealed class is to take away the inheritance feature from the user so they cannot derive a class from a sealed class. One of the best usage of sealed classes is when you have a class with static members. For example, the "Pens" and "Brushes" classes of the "System.Drawing" namespace. 

The Pens class represents the pens for standard colors. This class has only static members. For example, "Pens.Blue" represents a pen with the blue color. Similarly, the "Brushes" class represents standard brushes. "Brushes.Blue" represents a brush with blue color.
 

So when you're designing your application, you may keep in mind that you have sealed classes to seal the user's boundaries. 




Thank you for taking your time to look at my blog- Sadiq Ali Syed.








Using


USING BLOCK


Generally in our applications we will write code like create connection object to handle connectionstring after that open a connection and create command object etc. to interact with database to get data that would be like this 


It’s very easy way to write above code but problem is SqlConnection and SqlCommand objects will create IDISPOSABLE interface that means it could create unmanaged resources in our application to cleanup those objects we need to call Dispose() method at the end of our process otherwise those objects will remain in our application.

Suppose we use using statement in our applications it will automatically create try / finally blocks for the objects and automatically runs Dispose() method for us no need to create any try/finally block and no need to run any Dispose() method.

If we use using statement we need to write code will be like this 

C# Code



In above code using statement no need to worry about close connection because using statement automatically close the connection once process is complete and automatically it will run Dispose() method to dispose objects. 

The above using statement code will equal to below code








Thank you for taking your valuable time look at my blog - Sadiq Ali Syed




Wednesday 7 December 2016

Named Vs Optional Parameters


Optional Parameters

In C# 4.0 Parameters could be either required or optional. Now as a functional call, only required parameter is needed to pass. Optional Parameter, if not passed  will take default value.

Syntax





Key Points about Optional Parameter:
  • Each Optional Parameter has a default value as its part of the definition.
  • If no argument is sent for the Optional Parameter default is being used.
  • Default value of the Optional Parameter must be constant


  • Optional Parameter must define at the end of the any required parameter.

  • Optional Parameter could be applied on Constructor, Method, and Indexer etc.




Named Parameters

One case where optional parameters run into trouble is when there is more than one optional parameter with the same data type. For example:

public void DoSomething(int a = 5, int b = 10)
{
        ...
}


In this case, it's not clear what a call to DoSomething(37) means. Are you passing in a or b? This is where named arguments come in handy. Named parameters (also new to .NET 4.0) allow you to specify which parameter you intended.

// a = 37.  b = the default value.
DoSomething(a: 37);
 
// a = the default value.  b = 37.
DoSomething(b: 37);






Sunday 4 December 2016

AngularJS Test

My Quiz


  1. AngularJS directives are used in ________.

  2. Model
    View
    Controller
    Module

  3. Which of the following is a valid AngularJS expression?

  4. {{ 2 + 2 }}
    { 2 + 2 }
    (( 2 + 2 ))
    { (2 + 2) }

  5. Which of the following statements are true?

  6. Expression cannot contain condition, loop or RegEx
    Expression cannot declare a function
    Expression cannot contain comma, void or return keyword
    All of the above

  7. What is $scope?

  8. It transfers data between a controller and view
    It transfers data between model and controller
    It is a global scope in AngularJS.
    None of the above

  9. AngularJS filters ___________.

  10. Format the data without changing original data
    bind the data to display on UI
    Fetch the data from remote server
    Cache the subset of data on the browser

  11. AngularJS module can be created using ________.

  12. angular.module();
    var myModule = new module();
    module.create();
    angular.create();

  13. Which of the following statements are true?

  14. AngularJS controller maintains application data & behaviour using $scope
    AngularJS controller can be created in separate JS file
    AngularJS controller can be added into module
    All of the above

  15. Which of the followings are validation directives?

  16. ng-required
    ng-minlength
    ng-pattern
    All of the above

  17. How to acheive SPA in AngularJS?

  18. By using Routing
    By using Modules
    By using services
    None

  19. Angular JS hadles Dependecy injection by default?

  20. True
    False
    May be
    None