ASP.NET


Caches

A cache is the most valuable feature that Microsoft provides. It is a type of memory that is relatively small but can be accessed very quickly. It essentially stores information that is likely to be used again. For example, web browsers typically use a cache to make web pages load faster by storing a copy of the webpage files locally, such as on your local computer.



Starting

Caching is the process of storing data into cache.Working. Caching with the C# language is very easy. System.Runtime.Caching.dll provides the feature for working with caching in C#. In this illustration I am using the following classes:

ObjectCache
MomoryCache
CacheItemPolicy

ObjectCache: The CacheItem class provides a logical representation of a cache entry, that can include regions using the RegionName property. It exists in the System.Runtime.Caching.

MomoryCache: This class also comes under System.Runtime.Caching and it represents the type that implements an in-cache memory.

CacheItemPolicy: Represents a set of eviction and expiration details for a specific cache entry.

namespace CachingWithConsoleApplication  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            StockItems PS = new StockItems();  
         List<string> Pizzas= (List<string>)  PS.GetAvailableStocks();  
         Pizzas = (List<string>)PS.GetAvailableStocks();  
        }  
    }  
 
    public class StockItems  
    {  
        private const string CacheKey = "availableStocks";  
 
        public IEnumerable GetAvailableStocks()  
        {  
            ObjectCache cache = MemoryCache.Default;  
 
            if (cache.Contains(CacheKey))  
                return (IEnumerable)cache.Get(CacheKey);  
            else  
            {  
                IEnumerable availableStocks = this.GetDefaultStocks();  
 
                // Store data in the cache  
                CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();  
                cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(1.0);  
                cache.Add(CacheKey, availableStocks, cacheItemPolicy);  
 
                return availableStocks;  
            }  
        }  
        public IEnumerable GetDefaultStocks()  
        {  
            return new List<string>() { "Pen", "Pencil", "Eraser" };  
        }  
    }  
 
}

Advantages of Cache vs Session

One important difference is, that items in the cache can expire (will be removed from cache) after a specified amount of time. Items put into a session will stay there, until the session ends.

ASP.NET can also remove items from cache when the amount of available memory gets small.

Another difference: the session state can be kept external (state server, SQL server) and shared between several instances of your web app (for load balancing). This is not the case with the cache.

Besides of these differences (as others have noted): session is per user/session while cache is per application.


Another important difference, Session State will be blocked if concurrent async Ajax requests are executed, it will effect performance


Cache is in the Application scope with the purpose of reducing the number of times a piece of data is obtained. Session is in a user's session scope with the purpose of giving a particular user state.

Difference Between Unit Testing and Test Driven Development

Unit Testing refers to what you are testing, TDD to when you are testing.

The two are orthogonal.

Unit Testing means, well, testing individual units of behavior. An individual unit of behavior is the smallest possible unit of behavior that can be individually tested in isolation. (I know that those two definitions are circular, but they seem to work out quite well in practice.)

You can write unit tests before you write your code, after you write your code or while you write your code.

TDD means (again, kind of obvious) letting your tests drive your development (and your design). You can do that with unit tests, functional tests and acceptance tests. Usually, you use all three.

The most important part of TDD is the middle D. You let the tests drive you. The tests tell you what to do, what to do next, when you are done. They tell you what the API is going to be, what the design is. (This is important: TDD is not about writing tests first. There are plenty of projects that write tests first but don't practice TDD. Writing tests first is simply a prerequisite for being able to let the tests drive the development.)

Difference between “Layers” and “Tiers”?

layers refer to logical seperation of code. Logical layers help you organise your code better. For example an application can have the following layers.

1)Presentation Layer or UI Layer 2)Business Layer or Business Logic Layer 3)Data Access Layer or Data Layer

The aboove three layers reside in their own projects, may be 3 projects or even more. When we compile the projects we get the respective layer DLL. So we have 3 DLL's now.

Depending upon how we deploy our application, we may have 1 to 3 tiers. As we now have 3 DLL's, if we deploy all the DLL's on the same machine, then we have only 1 physical tier but 3 logical layers.

If we choose to deploy each DLL on a seperate machine, then we have 3 tiers and 3 layers.

So, Layers are a logical separation and Tiers are a physical separation. We can also say that, tiers are the physical deployment of layers.



Definition - What does Annotation mean?

Annotation is a term used in computer programming to refer to documentation and comments that may be found on code logic. Annotation is typically ignored once the code is executed or compiled.

Sometimes programmers will anticipate that those learning a programming language such as HTML, or those who may be modifying the programming at a later date, will require an explanation of the rationale behind the logic or even an explanation of how the logic accomplishes its purpose or goal. Thus, an annotation, or explanation, will be included within the code.

using Statement

 using (Font font1 = new Font("Arial", 10.0f)) 
        {
            byte charset = font1.GdiCharSet;
        }


File and Font are examples of managed types that access unmanaged resources (in this case file handles and device contexts). There are many other kinds of unmanaged resources and class library types that encapsulate them. All such types must implement the IDisposable interface.

As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.
The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object):

{
              Font font1 = new Font("Arial", 10.0f);
              try
              {
                byte charset = font1.GdiCharSet;
              }
              finally
              {
                if (font1 != null)
                  ((IDisposable)font1).Dispose();
              }
            }


The Agile Movement

What Is Agile?
Not a methodology! The Agile movement seeks alternatives to traditional project management. Agile approaches help teams respond to unpredictability through incremental, iterative work cadences and empirical feedback. Agilists propose alternatives to waterfall, or traditional sequential development.

What is Scrum?
Scrum is the most popular way of introducing Agility due to its simplicity and flexibility. Because of this popularity, many organizations claim to be “doing Scrum” but aren’t doing anything close to Scrum’s actual definition. Scrum emphasizes empirical feedback, team self management, and striving to build properly tested product increments within short iterations. Doing Scrum as it’s actually defined usually comes into conflict with existing habits at established non-Agile organizations.

Scrum has only three roles: Product Owner, Team, and Scrum Master. These are described in detail by the Scrum Training Series. The responsibilities of the traditional project manager role are split up among these three Scrum roles. Scrum has five meetings: Backlog Grooming (aka Backlog Refinement), Sprint Planning, Daily Scrum (aka 15-minute standup), the Sprint Review Meeting, and the Sprint Retrospective Meeting.

Page Life cycle


MVC page life cycle






CLR Diagram


What is View State?

Answer: View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.

A web application is stateless. That means that a new instance of a page is created every time when we make a request to the server to get the page and after the round trip our page has been lost immediately

Features of View State

These are the main features of view state:
Retains the value of the Control after post-back without using a session.
Stores the value of Pages and Control Properties defined in the page.
Creates a custom View State Provider that lets you store View State Information in a SQL Server Database or in another data store.
Advantages of View State

Easy to Implement.
No server resources are required: The View State is contained in a structure within the page load.
Enhanced security features: It can be encoded and compressed or Unicode implementation.



State Management


What is tracing in .NET?


nswer: Tracing helps to see the information of issues at the runtime of the application. By default Tracing is disabled.

Tracing has the following important features:

We can see the execution path of the page and application using the debug statement.
We can access and manipulate trace messages programmatically.
We can see the most recent tracing of the data.
Tracing can be done with the following 2 types.

Page Level: When the trace output is displayed on the page and for the page-level tracing we need to set the property of tracing at the page level.

<%@ Page Trace="true" Language="C#"

Application: Level: In Application-Level tracing the information is stored for each request of the application. The default number of requests to store is 10. But if you want to increase the number of requests and discard the older request and display a recent request then you need to set the property in the web.config file.

** Enum Struct are value type
**string string builder are reference type

What is the difference between “continue” and “break” statements in C#?

Using break statement, you can 'jump out of a loop' whereas by using continue statement, you can 'jump over one iteration' and then resume your loop execution.


Immutable :

When you do some operation on a object, it creates a new object hence state is not modifiable as in case of string.


Mutable

When you perform some operation on a object, object itself modified no new obect created as in case of StringBuilder

Constant and read only


Constant (const) and Readonly (readonly) both looks like same as per the uses but they have some differences:

Constant is known as “const” keyword in C# which is also known immutable values which are known at compile time and do not change their values at run time like in any function or constructor for the life of application till the application is running.

Readonly is known as “readonly” keyword in C# which is also known immutable values and are known at compile and run time and do not change their values at run time like in any function for the life of application till the application is running. You can assay their value by constructor when we call constructor with “new” keyword.


ASP.NET


Can “this” be used within a static method


We can't use this in static method because keyword 'this' returns a reference to the current instance of the class containing it. Static methods (or any static member) do not belong to a particular instance. They exist without creating an instance of the class and call with the name of a class not by instance so we can’t use this keyword in the body of static Methods, but in case of Extension Methods we can use it the functions parameters. Let’s have a look on “this” keyword.

The "this" keyword is a special type of reference variable that is implicitly defined within each constructor and non-static method as a first parameter of the type class in which it is defined. For example, consider the following class written in C#.


Define Property in C#.net?


Answer:

Properties are members that provide a flexible mechanism to read, write or compute the values of private fields, in other words by the property we can access private fields. In other words we can say that a property is a return type function/method with one parameter or without a parameter. These are always public data members. It uses methods to access and assign values to private fields called accessors.

Now question is what are accessors?

The get and set portions or blocks of a property are called accessors. These are useful to restrict the accessibility of a property, the set accessor specifies that we can assign a value to a private field in a property and without the set accessor property it is like a read-only field. By the get accessor we can access the value of the private field, in other words it returns a single value. A Get accessor specifies that we can access the value of a field publically.


What is the difference between dispose and finalize methods in c#?


Answer: finalizer and dispose both are used for same task like to free unmanaged resources but have some differences see.

Finalize:

Finalize used to free unmanaged resources those are not in use like files, database connections in application domain and more, held by an object before that object is destroyed.
In the Internal process it is called by Garbage Collector and can’t called manual by user code or any service.
Finalize belongs to System.Object class.
Implement it when you have unmanaged resources in your code, and make sure that these resources are freed when the Garbage collection happens.

Dispose:

Dispose is also used to free unmanaged resources those are not in use like files, database connections in Application domain at any time.
Dispose explicitly it is called by manual user code.
If we need to dispose method so must implement that class by IDisposable interface.
It belongs to IDisposable interface.
Implement this when you are writing a custom class that will be used by other users.


What is delegates in C# and uses of delegates


C# delegates are same as pointers to functions, in C or C++. A delegate Object is a reference type variable that use to holds the reference to a method. The reference can be changed at runtime which is hold by an object of delegate, a delegate object can hold many functions reference which is also known as Invocation List that refers functions in a sequence FIFO, we can new functions ref in this list at run time by += operator and can remove by -= operator.

Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.

ASP.NET


What is 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. In Visual Basic .NET the Not Inheritable keyword serves the purpose of sealed. If a class is derived from a sealed class then the compiler throws an error.


What are partial classes


A partial class is only use to splits the definition of a class in two or more classes in a same source code file or more than one source files. You can create a class definition in multiple files but it will be compiled as one class at run time and also when you’ll create an instance of this class so you can access all the methods from all source file with a same object.

Partial Classes can be create in the same namespace it’s doesn’t allowed to create a partial class in different namespace. So use “partial” keyword with all the class name which you want to bind together with the same name of class in same namespace, let’s have an example




What is boxing and unboxing?


Boxing:

Boxing is the process of converting a value type data type to the object or to any interface data type which is implemented by this value type. When the CLR boxes a value means when CLR converting a value type to Object Type, it wraps the value inside a System.Object and stores it on the heap area in application domain.

Unboxing:

Unboxing is also a process which is use to extracts the value type from the object or any implemented interface type. Boxing may be done implicit but unboxing have to be explicit by code.

What is IEnumerable


IEnumerable is the parent interface for all non-generic collections in System.Collections namespace like ArrayList, HastTable etc. that can be enumerated. For the generic version of this interface as IEnumerable<T> which a parent interface of all generic collections class in System.Collections.Generic namespace like List<> and more.

Late binding and early binding

Early Binding and Late Binding concepts belongs to polymorphism so let’s see first about polymorphism:

Polymorphism is an ability to take more than one form of a function means with a same name we can write multiple functions code in a same class or any derived class.

Polymorphism we have 2 different types to achieve that:

Compile Time also known as Early Binding or Overloading.
Run Time also known as Late Binding or Overriding.
Compile Time Polymorphism or Early Binding:

In Compile time polymorphism or Early Binding we will use multiple methods with same name but different type of parameter or may be the number or parameter because of this we can perform different-different tasks with same method name in the same class which is also known as Method overloading.

Run Time Polymorphism or Late Binding:

Run time polymorphism also known as late binding, in Run Time polymorphism or Late Binding we can do use same method names with same signatures means same type or same number of parameters but not in same class because compiler doesn’t allowed that at compile time so we can use in derived class that bind at run time when a child class or derived class object will instantiated that’s way we says that Late Binding. For that we have to create my parent class functions as partial and in driver or child class as override functions with override keyword.

What are the differences between IEnumerable and IQueryable


IEnumerable:

Is the parent interface for all non-generic collections in System.Collections namespace like ArrayList, HastTable etc. that can be enumerated. For the generic version of this interface as IEnumerable<T> which a parent interface of all generic collections class in System.Collections.Generic namespace like List<> and more.

IQueryable:

As per MSDN IQueryable interface is intended for implementation by query providers. It is only supposed to be implemented by providers that also implement IQueryable<T>. If the provider does not also implement IQueryable<T>, the standard query operators cannot be used on the provider's data source.

The IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed. The definition of "executing an expression tree" is specific to a query provider. For example, it may involve translating the expression tree to an appropriate query language for the underlying data source. Queries that do not return enumerable results are executed when the Execute method is called.

ASP.NET


What happens if the inherited interfaces have conflicting method names



If we implement multipole interface in the same class with conflict method name so we don’t need to define all or in other words we can say if we have conflict methods in same class so we can’t implement their body independently in the same class coz of same name and same signature so we have to use interface name before method name to remove this method confiscation let’s see an example:

interface testInterface1 {
    void Show();
}
interface testInterface2 {
    void Show();
}
class Abc: testInterface1,
testInterface2 {

    void testInterface1.Show() {
        Console.WriteLine("For testInterface1 !!");
    }
    void testInterface2.Show() {
        Console.WriteLine("For testInterface2 !!");
    }
}

Difference between is and as operator 


"is" operator

In the C# language, we use the "is" operator to check the object type. If the two objects are of the same type, it returns true and false if not.

"as" operator:

The "as" operator behaves similar to the "is" operator. The only difference is it returns the object if both are compatible to that type else it returns null.


What are the Difference between Array and ArrayList


ASP.NET


No comments:

Post a Comment