Showing posts with label asp.net ebook. Show all posts
Showing posts with label asp.net ebook. Show all posts

ASP.NET Interview questions and answers for classes struct methods

ASP.NET Interview questions and answers for  classes struct methods

What is a partial class. Give an example?
A partial class is a class whose definition is present in 2 or more files. Each source file contains a section of the class, and all parts are combined when the application is compiled. To split a class definition, use the partial keyword as shown in the example below. Student class is split into 2 parts. The first part defines the study() method and the second part defines the Play() method. When we compile this program both the parts will be combined and compiled. Note that both the parts uses partial keyword and public access modifier.
using System;
namespace PartialClass
{
  public partial class Student
  {
    public void Study()
    {
      Console.WriteLine("I am studying");
    }
  }
  public partial class Student
  {
    public void Play()
    {
      Console.WriteLine("I am Playing");
    }
  }
  public class Demo
  {
    public static void Main()
    {
      Student StudentObject = new Student();
      StudentObject.Study();
      StudentObject.Play();
    }
  }
}
It is very important to keep the following points in mind when creating partial classes.
1. All the parts must use the partial keyword.
2. All the parts must be available at compile time to form the final class.
3. All the parts must have the same access modifiers - public, private, protected etc.
4. Any class members declared in a partial definition are available to all the other parts.
5. The final class is the combination of all the parts at compile time.

What are the advantages of using partial classes?
1. When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.

2. When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.

Is it possible to create partial structs, interfaces and methods?
Yes, it is possible to create partial structs, interfaces and methods. We can create partial structs, interfaces and methods the same way as we create partial classes.
Will the following code compile?
using System;
namespace PartialClass
{
  public partial class Student
  {
    public void Study()
    {
      Console.WriteLine("I am studying");
    }
  }
  public abstract partial class Student
  {
    public void Play()
    {
      Console.WriteLine("I am Playing");
    }
  }
  public class Demo
  {
    public static void Main()
    {
      Student StudentObject = new Student();
    }
  }
}
No, a compile time error will be generated stating "Cannot create an instance of the abstract class or interface "PartialClass.Student". This is because, if any part is declared abstract, then the whole class becomes abstract. Similarly if any part is declared sealed, then the whole class becomes sealed and if any part declares a base class, then the whole class inherits that base class.

Can you create partial delegates and enumerations?
No, you cannot create partial delegates and enumerations.

Can different parts of a partial class inherit from different interfaces?
Yes, different parts of a partial class can inherit from different interfaces.

Can you specify nested classes as partial classes?
Yes, nested classes can be specified as partial classes even if the containing class is not partial. An example is shown below.
class ContainerClass
{
  public partial class Nested
  {
    void Test1() { }
  }
  public partial class Nested
  {
    void Test2() { }
  }
}
How do you create partial methods?
To create a partial method we create the declaration of the method in one part of the partial class and implementation in the other part of the partial class. The implementation is optional. If the implementation is not provided, then the method and all the calls to the method are removed at compile time. Therefore, any code in the partial class can freely use a partial method, even if the implementation is not supplied. No compile-time or run-time errors will result if the method is called but not implemented. In summary a partial method declaration consists of two parts. The definition, and the implementation. These may be in separate parts of a partial class, or in the same part. If there is no implementation declaration, then the compiler optimizes away both the defining declaration and all calls to the method.

The following are the points to keep in mind when creating partial methods.
1. Partial method declarations must begin partial keyword.
2. The return type of a partial method must be void.
3. Partial methods can have ref but not out parameters.
4. Partial methods are implicitly private, and therefore they cannot be virtual.
5. Partial methods cannot be extern, because the presence of the body determines whether they are defining or implementing.

What is the use of partial methods?
Partial methods can be used to customize generated code. They allow for a method name and signature to be reserved, so that generated code can call the method but the developer can decide whether to implement the method. Much like partial classes, partial methods enable code created by a code generator and code created by a human developer to work together without run-time costs.

Keywords:
sealed and partial class in c#
c# partial class different namespace
c# partial class inheritance
partial methods in c#
partial class in c# javatpoint
c# partial class constructor
c# partial class best practices
partial class in mvc
c# partial interface
c# partial method
sealed and partial class in c#
partial class in c# stackoverflow
c# partial class best practices
c# partial class constructor
partial class in mvc
c# partial class inheritance
partial class in c# stackoverflow
c# partial method
sealed and partial class in c#
c# partial class constructor
c# partial class inheritance
c# partial interface
partial class in c# javatpoint
c# partial class different namespace
c# partial class inheritance
partial class in c# stackoverflow
sealed and partial class in c#
c# partial class constructor
c# partial method
partial class in mvc
c# partial class different namespace
partial class in c# javatpoint
partial class in c# stackoverflow
partial class in mvc
c# nested class
c# partial class best practices
c# partial class inheritance
sealed and partial class in c#
partial class in c# javatpoint
c# partial method
c# partial method multiple implementations
c# partial method vs abstract
partial class in c#
extension methods in c#
no defining declaration found for implementing declaration of partial method
c# partial class constructor
c# partial class best practices
delegates in c#

ASP.NET Interview questions and answers for interfaces

ASP.NET Interview questions and answers for  interfaces

Explain what is an Interface in C#?
An Interface in C# is created using the interface keyword. An example is shown below.
using System;
namespace Interfaces
{
   interface IBankCustomer
   {
      void DepositMoney();
      void WithdrawMoney();
   }
   public class Demo : IBankCustomer
   {
      public void DepositMoney()
      {
         Console.WriteLine("Deposit Money");
      }

      public void WithdrawMoney()
      {
         Console.WriteLine("Withdraw Money");
      }

      public static void Main()
      {
         Demo DemoObject = new Demo();
         DemoObject.DepositMoney();
         DemoObject.WithdrawMoney();
      }
   }
}
In our example we created IBank Customer interface. The interface declares 2 methods.
1. void DepositMoney();
2. void WithdrawMoney();

Notice that method declarations does not have access modifiers like public, private, etc. By default all interface members are public. It is a compile time error to use access modifiers on interface member declarations. Also notice that the interface methods have only declarations and not implementation. It is a compile time error to provide implementation for any interface member. In our example as the Demo class is inherited from the IBankCustomer interface, the Demo class has to provide the implementation for both the methods (WithdrawMoney() and DepositMoney()) that is inherited from the interface. If the class fails to provide implementation for any of the inherited interface member, a compile time error will be generated. Interfaces can consist of methods, properties, events, indexers, or any combination of those four member types. When a class or a struct inherits an interface, the class or struct must provide implementation for all of the members declared in the interface. The interface itself provides no functionality that a class or struct can inherit in the way that base class functionality can be inherited. However, if a base class implements an interface, the derived class inherits that implementation.

Can an Interface contain fields?
No, an Interface cannot contain fields.

What is the difference between class inheritance and interface inheritance?
Classes and structs can inherit from interfaces just like how classes can inherit a base class or struct. However there are 2 differences.
1. A class or a struct can inherit from more than one interface at the same time where as A class or a struct cannot inherit from more than one class at the same time. An example depicting the same is shown below.
using System;
namespace Interfaces
{
   interface Interface1
   {
      void Interface1Method();
   }
   interface Interface2
   {
      void Interface2Method();
   }
   class BaseClass1
   {
      public void BaseClass1Method()
      {
         Console.WriteLine("BaseClass1 Method");
      }
   }
   class BaseClass2
   {
      public void BaseClass2Method()
      {
         Console.WriteLine("BaseClass2 Method");
      }
   }

   //Error : A class cannot inherit from more than one class at the same time
   //class DerivedClass : BaseClass1, BaseClass2
   //{
   //}

   //A class can inherit from more than one interface at the same time
   public class Demo : Interface1, Interface2
   {
      public void Interface1Method()
      {
         Console.WriteLine("Interface1 Method");
      }

      public void Interface2Method()
      {
         Console.WriteLine("Interface2 Method");
      }

      public static void Main()
      {
         Demo DemoObject = new Demo();
         DemoObject.Interface1Method();
         DemoObject.Interface2Method();
      }
   }
}
2. When a class or struct inherits an interface, it inherits only the method names and signatures, because the interface itself contains no implementations.

Can an interface inherit from another interface?
Yes, an interface can inherit from another interface. It is possible for a class to inherit an interface multiple times, through base classes or interfaces it inherits. In this case, the class can only implement the interface one time, if it is declared as part of the new class. If the inherited interface is not declared as part of the new class, its implementation is provided by the base class that declared it. It is possible for a base class to implement interface members using virtual members; in that case, the class inheriting the interface can change the interface behavior by overriding the virtual members.

Can you create an instance of an interface?
No, you cannot create an instance of an interface.

If a class inherits an interface, what are the 2 options available for that class?
Option 1: Provide Implementation for all the members inheirted from the interface.
namespace Interfaces
{
   interface Interface1
   {
      void Interface1Method();
   }

   class BaseClass1 : Interface1
   {
      public void Interface1Method()
      {
         Console.WriteLine("Interface1 Method");
      }
      public void BaseClass1Method()
      {
         Console.WriteLine("BaseClass1 Method");
      }
   }
}
Option 2: If the class does not wish to provide Implementation for all the members inheirted from the interface, then the class has to be marked as abstract.
namespace Interfaces
{
   interface Interface1
   {
      void Interface1Method();
   }

   abstract class BaseClass1 : Interface1
   {
      abstract public void Interface1Method();
      public void BaseClass1Method()
      {
         Console.WriteLine("BaseClass1 Method");
      }
   }
}
A class inherits from 2 interfaces and both the interfaces have the same method name as shown below. How should the class implement the drive method for both Car and Bus interface?
namespace Interfaces
{
   interface Car
   {
      void Drive();
   }
   interface Bus
   {
      void Drive();
   }

   class Demo : Car,Bus
   {
      //How to implement the Drive() Method inherited from Bus and Car
   }
}
To implement the Drive() method use the fully qualified name as shown in the example below. To call the respective interface drive method type cast the demo object to the respective interface and then call the drive method.
using System;
namespace Interfaces
{
   interface Car
   {
      void Drive();
   }
   interface Bus
   {
      void Drive();
   }

   class Demo : Car,Bus
   {
      void Car.Drive()
      {
         Console.WriteLine("Drive Car");
      }
      void Bus.Drive()
      {
         Console.WriteLine("Drive Bus");
      }

      static void Main()
      {
         Demo DemoObject = new Demo();
         ((Car)DemoObject).Drive();
         ((Bus)DemoObject).Drive();
      }
   }
}

What do you mean by "Explicitly Implementing an Interface". Give an example?
If a class is implementing the inherited interface member by prefixing the name of the interface, then the class is "Explicitly Implemeting an Interface member". The disadvantage of Explicitly Implemeting an Interface member is that, the class object has to be type casted to the interface type to invoke the interface member. An example is shown below.
using System;
namespace Interfaces
{
   interface Car
   {
      void Drive();
   }

   class Demo : Car
   {
      // Explicit implementation of an interface member
      void Car.Drive()
      {
         Console.WriteLine("Drive Car");
      }

      static void Main()
      {
         Demo DemoObject = new Demo();

         //DemoObject.Drive();
         // Error: Cannot call explicitly implemented interface method
         // using the class object.
         // Type cast the demo object to interface type Car
         ((Car)DemoObject).Drive();
      }
   }
}

Keywords:
interface in c# with example code project
types of interface in c#
interface in c# with real time example
multiple interface in c#
interface in c# interview questions
interface in c# tutorialspoint
difference between abstract class and interface in c#
c# interface property
interface inheritance c#
difference between abstract class and interface in c#
types of interface in c#
c# instantiate interface
interface inheritance vs implementation inheritance
interface with implementation c#
c# struct interface
single inheritance in c#
c# interface tutorial
types of interface in c#
c# interface inheritance override
c# interface constructor
c# interface property
interface in c# with example code project
multiple interface in c#
c# multiple inheritance
can we create instance of interface in c#
can we create instance of interface in java
can we create object of interface in java 8
interface instance c#
many classes can implement the same interface
can we create object for interface
can we create instance of abstract class
a class can implement multiple interfaces
c# interface
tricky question on interface in c#
use of interface in c#
interface in asp net
types of interface in c#
interview questions on inheritance in c# for experienced
user interface in c#
abstract class in c#

ASP.NET Interview questions and answers for http module

ASP.NET Interview questions and answers for http module

What is an HTTP Handler?
An ASP.NET HTTP handler is the process (frequently referred to as the "endpoint") that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler. You can create your own HTTP handlers that render custom output to the browser.
What is HTTP module?
An HTTP module is an assembly that is called on every request that is made to your application. HTTP modules are called as part of the ASP.NET request pipeline and have access to life-cycle events throughout the request. HTTP modules let you examine incoming and outgoing requests and take action based on the request.
What is the interface that you have to implement if you have to create a Custom HTTP Handler?
Implement IHttpHandler interface to create a synchronous handler.
Implement IHttpAsyncHandler to create an asynchronous handler.

What is the difference between asynchronous and synchronous HTTP Handlers?

A synchronous handler does not return until it finishes processing the HTTP request for which it is called.
An asynchronous handler runs a process independently of sending a response to the user. Asynchronous handlers are useful when you must start an application process that might be lengthy and the user does not have to wait until it finishes before receiving a response from the server.

Which class is responsible for receiving and forwarding a request to the appropriate HTTP handler?

IHttpHandlerFactory Class

Can you create your own custom HTTP handler factory class?
Yes, we can create a custom HTTP handler factory class by creating a class that implements the IHttpHandlerFactory interface.
What is the use of HTTP modules?
HTTP modules are used to implement various application features, such as forms authentication, caching, session state, and client script services.

What is the difference between HTTP modules and HTTP handlers?

An HTTP handler returns a response to a request that is identified by a file name extension or family of file name extensions. In contrast, an HTTP module is invoked for all requests and responses. It subscribes to event notifications in the request pipeline and lets you run code in registered event handlers. The tasks that a module is used for are general to an application and to all requests for resources in the application.
What is the common way to register an HTTP module?
The common way to register an HTTP module is to have an entry in the application's Web.config file.

Much of the functionality of a module can be implemented in a global.asax file. When do you create an HTTP module over using Global.asax File?
You create an HTTP module over using Global.asax file if the following conditions are true

1. You want to re-use the module in other applications.
2. You want to avoid putting complex code in the Global.asax file.
3. The module applies to all requests in the pipeline.

Keywords:
what is http module in asp.net c#
iis http module
custom http modules
http module and http handler difference
iis http module authentication
mvc httpmodule
httphandler and httpmodule in asp net kudvenkat
httpmodule and httphandler details and difference between them
http module and http handler difference
httphandler c#
httpmodule and httphandler details and difference between them
iis http module
how can we create instance of http module
custom http modules
context.handler in asp.net c#
mvc httpmodule
httphandler c#
context handler in asp net c#
http handler java
http.handler golang
proper http handler order in request pipeline
http handlers in .net core
iis http module
.net framework httphandler
http module and http handler difference
iis http module authentication

ASP.NET Interview questions Data security

ASP.NET Interview questions Data security

What are the best practices to follow to secure connection strings in an ASP.NET web application?1. Always store connection strings in the site's Web.config file. Web.config is very secure. Users will not be able to access web.config from the browser.
2. Do not store connection strings as plain text. To help keep the connection to your database server secure, it is recommended that you encrypt connection string information in the configuration file.
3. Never store connection strings in an aspx page.
4. Never set connection strings as declarative properties of the SqlDataSource control or other data source controls.

Why is "Connecting to SQL Server using Integrated Security" considered a best practice?
Connecting to SQL Server using integrated security instead of using an explicit user name and password, helps avoid the possibility of the connection string being compromised and your user ID and password being exposed.
What is the advantage of storing an XML file in the applications App_Data folder? 
The contents of the App_Data folder will not be returned in response to direct HTTP requests.
What is Script injection?
A script injection attack attempts to send executable script to your application with the intent of having other users run it. A typical script injection attack sends script to a page that stores the script in a database, so that another user who views the data inadvertently runs the code.

What is SQL injection?
A SQL injection attack attempts to compromise your database by creating SQL commands that are executed instead of, or in addition to, the commands that you have built into your application.

What are the best practices to keep in mind when accepting user input on a web application?
1. Always use validation controls whenever possible to limit user input to acceptable values.
2. Always check the IsValid property of the aspx page. Run the server side code only if the IsValid property value is true. A value of false means that one or more validation controls have failed a validation check.
3. Always perform server side validation irrespective of client side validation being performed or not. This will protect your web application even if the client has by passed the client side validation by disabling javascript in the web browser.
4. Also make sure to re validate user input in the business logic layer of your application.
What are the steps to follow to avoid Script Injection attacks?
1. Encode user input with the HtmlEncode method. This method turns HTML into its text representation.
2. If you are using the GridView control with bound fields, set the BoundField object's HtmlEncode property to true. This causes the GridView control to encode user input when the row is in edit mode.

What are the steps to follow to avoid SQL Injection attacks?
Always use parameterized queries or stored procedures instead of creating SQL commands by concatenating strings together.

Can you encrypt view state data of an aspx page?
Yes, you encrypt view state data of an aspx page by setting the page's ViewStateEncryptionMode property to true.

Keywords:
asp.net mvc interview questions
asp.net mvc security interview questions
net interview questions for 6 years experience
c# interview questions
senior net developer interview questions and answers
web security interview questions
net telephonic interview questions
how to explain asp net mvc project in interview
script injection example
cross site scripting
code injection example
sql injection
cross site scripting tutorial
html injection
how to prevent script injection in java
client side json injection hackerone
sql injection types
how to prevent sql injection
sql injection code list
sql injection 1=1
sql injection php
sql injection test
sql injection owasp
sql injection password