If at first you don't succeed, call it version 1.0

Contact Me

Sarvesh Kushwaha
Email : sarveshkushwaha@outlook.com

Total Pageviews

Powered by Blogger.

Sunday, 12 February 2017

10 Essentials C# keywords every .Net developer should know before attending an Interview #AspNetInterviewQuestionsSeries


1. Var and/vs Dynamic

With Var keyword properties / Methods /Operators etc are resolve at compile time.
Var keyword is static typed and type cannot be change,Once you have assigned the type to var.

var i = "abc";
i = 1 ; //you cannot assign it integer value once it has been marked as string


With Dynamic keyword properties / Methods /Operators etc are resolve at runtime.
type can be change once it is already assigned.

dynamic i = "abc";
i = 1 ; //you can change the type in dynamic
Console.WriteLine(i.anything); //error will be given at runtime

2. Const and ReadOnly
  1. Constants are evaluated at the compile-time, while the read-only variables are evaluated at the runtime. 
  2. Constants support only value-type variables, while read-only variables can hold reference type variables. 
  3. Constants should be used when the value is not changing during the runtime, and read-only variables are used mostly when their actual value is unknown before the runtime.

static readonly string readonlystring;   //you can levae it blank and can assign value at runtime,assign object to it 
 const string constring ="abc"; // you have to assign some value to const while declaring,you can assign ref type to const

3. Checked and Unchecked keyword
Checked keyword is to check integral arithmetic overflow.
What will happen if you are assigning the greater value to your int than its maximum value?
you will get the wrong result,to check above condition and throw airtmetic overflow use Checked keyword either in block () or in expression {} manner.



//checked block
int checkedk = int.MaxValue;
  Console.WriteLine(checked(checkedk+ 20));
  
  //checked expression
   checked
   {
    int i = 10;
     int i3 = 2147483647 + i;
                Console.WriteLine(i3);
   }

unchecked keyword is just opposite of that it will not throw any exception. by default int ,byte are unchecked.

4. AS and IS keyword
Both keywords are used for safe cast typing with lill diff in their working.

IS : 
checks whether the type of an given object is compatible with the new object type and returns Boolean type true or false.

AS:
checks whether the type of an given object is compatible with the new object type and returns object if it is compatible or null if it isn't.

person p = new person();
     Men men = new Men();
  
  Men m2 =  p as Men;
  Console.WriteLine(p is Men);
  Console.WriteLine(men is person);
  if(m2 !=null)
  {
   Console.WriteLine("m is person type");
  }else
  {
     Console.WriteLine("m is not person type");
  }


5. Volatile and Lock keyword
volatile indicates that field can be modified by multiple thread. volatile keyword can be applied only to fields of class/struct.

Lock keyword ensures that if one thread is accessed any object in lock block that will not be accessible by another thread, it will have to wait until that thread release the accessed object.



Sunday, 29 January 2017

What are Func, Predicate, Action delegates with Examples ? #AspNetInterviewQuestionsSeries


To learn Delegate you guys can refer my previous article. In this article we will learn about what are Func, Predicate and Action Delegate.
Keeping the power of delegates in the mind .Net Framework has introduced the predefined delegates which can be useful for a developer. .Net Framework has also used these predefined delegates at many places e.g LINQ.

Action Delegate: As its name is ACTION this delegate ensures to performs actions and does not return anything. Action Delegate can take upto 16 parameters and returns nothing as they are void delegates and their primary task to perform some action. You can create Action in C# :

// define action 
Action<string> myAction;
// F12 definition will be of your action, it can go upto 16 parameters
public delegate void Action<in T>(T obj);
   


Example : they are most commonly getting used in List<T>.ForEach  like functions.

Friday, 27 January 2017

What are Delegates ? When and why we should use Delegates in C# with example ? #AspNetInterviewQuestionsSeries


Delegates :

Delegate word in English defines that entrust (a task or responsibility) to another person.Same concept is follows in C# with some additional properties.

Delegates Overview :
Delegates have the following properties:

  1. Delegates are similar to C++ function pointers, but are type safe. 
  2. Delegates allow methods to be passed as parameters. 
  3. Delegates can be used to define callback methods. 
  4. Delegates can be chained together; for example, multiple methods can be called on a single event. 
  5. Used as anonymous methods (inline code). 

Thursday, 26 January 2017

How to acess INTERNAL types and members into another assembly ? #AspNetInterviewQuestionsSeries


Lets learn about INTERNAL keyword in C#. Basically INTERNAL keyword is an access modifier which ensures that types and members within the same assembly. 

But is there any way we can access the INTERNAL  type and members from another assembly? 

yeah there is actually. We can use  the INTERNALVISIBLETO keyword into assmeblyinfo.cs or onto the class at namespace level using System.Runtime.CompilerServices.

Lets Code :

I have created two projects into a solution so we can have two diff assembly.






















Saturday, 21 January 2017

How to implement only few signature of an Interface ? #AspNetInterviewQuestionsSeries


lets make this question situational :) .An interviewer can ask you like this :


Lets suppose i am having an Interface and this Interface consisting five methods signatures.This Interface is already being consumed by our thousand of customer and now new customer came and asked that we need only three method inside that. what will you do to resolve this situation or what will you suggest to your customer ? 

Possible solutions are to this situation are as follows :

  1. Implement that Interface using an Abstract class and mark those methods as abstract which you don't want to implement.
  2. If class should be concrete then you can implement rest of the functions and throw exceptions like  NotImplementedException or NotSupportedException .
  3. Last you can segregate that Interface further into two interface and implement those in class.But then in above scenario it will become a tough situation.

Friday, 20 January 2017

How you can limit a class to create only two instances ? #AspNetInterviewQuestionsSeries


How you can limit a class to create only two(n) instances ?


This is one of my favorite question in an interview.So before watching the answer take a minute and think how you can create a such class .. tough ? Ahan its very easy.

 Answer : The basic idea is create a static property and increment that in constructor every time you are creating an object of that class. And throw an exception when that count is greater than two.

next question which can be ask is :

Sunday, 17 January 2016

Friday, 13 November 2015

Angular.js for ASP.NET MVC Developers- Basics


Fiuuuuuuuu After a long time I am again thinking to write a series.This time most popular JS library i.e angular.js for asp.net MVC developers.
Being an ASP.NET developer, I didn't find many resources on the internet for angular.js with AS.NET MVC.In this First blog, I will Introduce the angular.js for ASP.NET MVC guys.

What is Angular.js?


AngularJS is a client-side MVW(W stands for whatever) or MV* framework written in JavaScript. It runs in a web browser and greatly helps us (developers) to write modern, single-page, AJAX-style
web applications.
We can angular.js is an opinionated framework or software which tells or suggest us how to solve a problem in an efficient manner.Although there can be many solutions to a problem but angular feature specifications tells to do them in an efficient manner.



Sunday, 5 July 2015

Sunday, 29 March 2015

JavaScript For C# Developer



This Presentation depicts JavaScript concept for Csharp developer.It helps to understand the concepts of JavaScript resembling/differentiate them with C# concepts.

It took one week to complete the fundamental concepts of angular.Js, but it took more than one month to understand the concepts of javascript and relate it to C#.

So here it is:



Saturday, 3 January 2015

Hack proof your asp.net applications from Session Hijacking


Introduction:

This article is the Part-6 of my series Hack Proof your asp.net and asp.net mvc applications. In this article, I will describe what exactly Session Hijacking (Man-in the-middle-attack) is and how a hacker exploits it and how we can prevent Session Hijacking attack in asp.net applications.

Background:

Wednesday, 15 October 2014

Hack proof your Javascript using javascript Obfuscation in ASP.NET applications


Introduction:

This article is the Part-5 Article of my series Hack Proof your asp.net and asp.net mvc applications.
In this article i will describe how to obfuscate your JavaScript code (Your written business logic in JavaScript or those  JavaScript libraries you don't want to expose to others) in asp.net application with visual studio.

Background :

You can read previous article of this series from below links :

    1. Secure your ASP.NET applications from SQL Injection
    2. Secure your ASP.NET applications from XSS Attack
    3. Secure your ASP.NET applications from CSRF Attack
    4. Secure your ASP.NET applications from Sensitive Data Exposure and Information Leakage

    Thursday, 18 September 2014

    Sunday, 24 August 2014

    Application Initialization in IIS 7.5 and IIS 8


    Why we need Application initialization ?

    On first HTTP requests asp.net applications both asp.net web forms and asp.net mvc application requires initialization tasks and "warm up" tasks (Startup processing , generating contents) etc.

    Application initialization proactively loads and initialize all the dependencies like database connection , compilation of asp.net code and loading of assemblies etc...

    Benefits of Application initialization :

    • its decreases the first response time by preloading the application
    • Can set a static page to increase the user experience while application is initializing
    • Provides overlapped process recycling 

    Friday, 22 August 2014

    ASP.NET Precompilation for Deployment


    Benefits of Precompilation in ASP.NET:

    • Initial response of application will be faster for users, because code and files will not have to be compiled at the first time any user requested.
    • We can ensure error free application at compile time
    • From security concern people who have access to production server cannot see the source code and cannot make the changes(if you have chose the option of not updatable at the time of publish).
    • Large websites must use this precompilation.

    Friday, 15 August 2014

    Calculate the Execution time of a Function in C# , Razor View, Aspx View


    My previous article was on the how to do the performance profiling with Visual Studio. Using Visual Studio analyze tool we can profile a function , dynamic link library, project and solution.
    Link of  - Performance Profiling with Visual Studio

    Requirements :

    1. However if we want to see execution for a particular function , you definitely don't want to run the profiler for that.Although profiler is more sophisticated way.
    2. if you have any loop on your razor / aspx view of asp.net application ,through profile it wolld be difficult to calculate the time of that loop.

    Monday, 11 August 2014

    Performance Profiling in Visual Studio


    Hi Guys after a long time i am writing this article ( Feeling very poor [GARIB] ), Jokes apart In this article i will describe how to do the performance profiling using Visual Studio.
    There are many third party tools which do the Code Profiling , Memory profiling and Performance Profiling for us these are paid tools but they are good as well :
    • ANTS
    • Just Trace
    • Your KIT 
    • .NET Memory Profiler
    • Eqatec
    • Slim Tune Profiler
    • Dot Trace

    As i said am feeling poor, i will describe -"How to do Performance Profiling using Visual Studio inbuilt performance analysis tools ?"

    Sunday, 8 June 2014

    IE-10 right align text input issue


    Remove Clear Icon - "X" from input fields of Internet Explorer

    Recently i added style text-align to right of an input :
    <input type="text" style="text-align: right;">
    

    Issue :
    But when someone types number in that then the last part of the text inside the textbox isn't displayed completely in Internet Explorer 10 as soon as the textbox loses focus.It because of its "Clear Icon - x".
    Even they have fixed this in their latest version of Internet Explorere - 11.

    Friday, 6 June 2014

    Hack proof your ASP.NET applications from Sensitive Data Exposure and Information Leakage


    Introduction:

    This is Part 4 of my series Hack proof your asp.net application.In this article ,I will describe How we sometimes unintentionally expose some sensitive information or leak some information to a hacker , who used that information to hack us. Keeping These terms separate "Sensitive Data exposure" which can directly harm to an individual or an organization, "Information leakage" are which helps attacker to perform malicious activities.Both terms are correlated and we can say Information leakage can contain Sensitive data exposure and vice versa.

    Background:

    You can read previous article of this series from below links :

    Sunday, 13 April 2014

    What is Heartbleed bug and Its solutions ?


    Introduction 
    From last few days Heartbleed trending on the internet and saying to the internet, I am the evil. People are calling this bug as "Biggest Security Threat" to the internet. Some Websites called this bug "Catastrophic".
    I was Gawked to know i was not safe since 2011 December Since OpneSSL included Heartbeat Extension.