Archive

Archive for October, 2014

Error : The file you are attempting to save or retrieve has been blocked from this Web site by the server administrators.

Categories: Uncategorized

Error : Failed to establish connection with report server. Verify the server URL is correct or review ULS logs for more information. Product area: SQL Server Reporting Services, Category: Configuration Pages

Categories: Uncategorized

How do I get my solution in Visual Studio back online in TFS?

Categories: Uncategorized

.Net OOPs


Abstraction

Ability to define the functionality of the class in simplified manner. (Explain the functionality of the class in simplified manner)

Encapsulation

Encapsulation is the process of binding all data (variables and methods) inside object form outside of the world

Inheritance

Inheritance is the process of creation new classes from already existing classes

Syntax to inherit from a class in C#

Place a colon and then the name of the base class.

*******************************************************************************************************************

Example: class MyNewClass: MyBaseClass

using System;

public class BClass

{

public void bmethod()

{

Console.WriteLine(“This is base class method”);

}

}

public class ChClass:BClass

{

public void chmethod()

{

Console.WriteLine (“This is child class method”);

}

}

Public class sample

{     Public static void Main ()

{    ChClass ch=new ChClass ();

ch.chmethod ();

ch.bmethod ();

}

}

*******************************************************************************************************************

****We can prevent our class from being inherited by another class by using sealed keyword.

****we can allow a class to be inherited, but prevent the method from being over-ridden, Just leave the class public and make the method sealed

We can call a base class method without creating instance

  • It’s possible If it’s a static method.
  • It’s possible by inheriting from that class also.
  • It’s possible from derived classes using base keyword.

*******************************************************************************************************************

Example1:

using System;

public class BClass

{

public void bmethod()

{

Console.WriteLine(“This is base class method”);

}

}

public class ChClass:BClass

{

public void chmethod()

{

//Console.WriteLine(“This is child class method”);

base.bmethod(); //Its possible from derived classes using base keyword.

bmethod(); //Its possible by inheriting from that class also.

}

 

}

public class sample

{  public static void Main()

{

ChClass ch=new ChClass();

ch.chmethod();

}

}

Example2:

 

using System;

public class BClass

{     public  void bmethod()

{

Console.WriteLine(“This is base class method”);

}

private void bprivatemethod()

{

Console.WriteLine(“This is base class private method”);

}

}

public class ChClass:BClass  //We can inherit the private method from base  class but can’t access it.

{

public void chmethod()

{

Console.WriteLine(“This is child class method”);

}

 

}

 

public class sample

{

 

public static void Main()

{

ChClass ch=new ChClass();

//ch.bprivatemethod();

}

 

}

OutPut:

csc sample.cs

it compile successfully no errors

if I call ch.bprivatemethod(); then it shows error

sample.cs(32,2): error CS0122: ‘BClass.bprivatemethod()’ is inaccessible due to its protection level

*******************************************************************************************************************

Example 1:

using System;

 

public class sample //main calss access specifier should be public

{

 

private class BClass

{

public  void bmethod()

{

Console.WriteLine(“This is base class method”);

}

private void bprivatemethod()

{

Console.WriteLine(“This is base class private method”);

}

}

public class ChClass:BClass

{

public void chmethod()

{

Console.WriteLine(“This is child class method”);

}

 

}

 

public class test

{

public static void Main()

{

ChClass ch=new ChClass();

//ch. bprivatemethod();

}

 

}

 

}

Error:

sample.cs(17,14): error CS0060: Inconsistent accessibility: base class

‘sample.BClass’ is less accessible than class ‘sample.ChClass’

sample.cs(6,15): (Location of symbol related to previous error)

*******************************************************************************************************************

Sealed classs

It prevent to derive the new class from base class

Sealed methods

****Use of the sealed modifier prevents a derived class from further overriding the method.

****If an instance method declaration includes the sealed modifier, it must also include the override modifier.

 

*******************************************************************************************************************

Example 1:

 

using System;

public sealed class BClass    //We can prevent our class from being inherited by another class by using sealed keyword

{

public void bmethod()

{

Console.WriteLine(“This is base class method”);

}

}

public class ChClass1:BClass

{

public void ch1method()

{

Console.WriteLine(“This is child class1  method”);

 

}

 

 

}

public class sample

{

public static void Main()

{

ChClass1 ch1=new ChClass1();

}

 

}

error:

sample.cs(9,14): error CS0509: ‘ChClass1’ : cannot inherit from sealed class

‘BClass’

Example 2:

using System;

class A

{

public virtual void F() {

Console.WriteLine(“A.F”);

}

public virtual void G() {

Console.WriteLine(“A.G”);

}

}

class B: A

{

sealed override public void F() {

Console.WriteLine(“B.F”);

}

override public void G() {

Console.WriteLine(“B.G”);

}

}

class C: B

{

override public void G() {

Console.WriteLine(“C.G”);

}

}

 

Note:the class B provides two override methods: an F method that has the sealed modifier and a G method that does not. B’s use of the sealed modifier prevents C from further overriding F

*******************************************************************************************************************

Polymorphism

  • Polymorphism allows objects to be represented in multiple forms.
  • Polymorphism is the ability for an object to change its behavior according to how it is being used
  • Polymorphism means allowing a single definition to be used with different types of data

Method overloading

Overloading – is the concept of using one function or class in different ways by changing the signature of its parameters.

it occurs when a class contain more than one method with same name with different signature

 

These are the different ways a method can be overloaded

  • Different parameter data types.
  • Different number of parameters.
  • Different order of parameters.

 

*******************************************************************************************************************

Example 1:

Public Class Calc

{

public void fnMultiply(int x, int y)

{ return x * y;

}

public void fnMultiply(int x, int y, int z)

{ return x * y * z;

}

} … …

Calc obj; int Result; Result = obj.fnMultiply(2,3,4);

// The second fnMultiply would be called

Result = obj.fnMultiply(3,4);

// The first fnMultiply would be called //Here, the call depends on the number of parameters passed, polymorphism is achieved using overloading

 

*******************************************************************************************************************

Method overriding

Means base class and child class contain same method name with same signature.its alter the behaviour of members of a base class

*******************************************************************************************************************

Example 1:

using System;

public class BClass

{

public void bmethod()

{

Console.WriteLine(“This is base class method”);

}

}

public class ChClass:BClass

{

public void chmethod()

{

Console.WriteLine(“This is child class method”);

 

}

public void bmethod()

{

Console.WriteLine(“This is child class+base class method”);

}

}

public class sample

{

 

public static void Main()

{

ChClass ch=new ChClass();

ch.chmethod();

ch.bmethod();

}

 

}

output:

sample.cs(16,14): warning CS0108: The keyword new is required on

‘ChClass.bmethod()’ because it hides inherited member ‘BClass.bmethod()’

sample.cs(4,14): (Location of symbol related to previous warning)

 

C:\downloads> sample

This is child class method

This is child class+base class method

 

The member ‘BClass.bmethod()’ does not hide an inherited member.The new keyword is not required.

The keyword new is required on ‘ChClass.bmethod()’ because it hides inherited member ‘BClass.bmethod()’

*******************************************************************************************************************

New, virtual, override

C# allows derived classes to contain methods with the same name as base class methods.

  • The base class method must be defined virtual.
  • If the method in the derived class is not preceded by new or override keywords, the compiler will issue a warning and the method will behave as if the new keyword were present.
  • If the method in the derived class is preceded with the new keyword, the method is defined as being independent of the method in the base class.
  • If the method in the derived class is preceded with the override keyword, objects of the derived class will call that method rather than the base class method.
  • The base class method can be called from within the derived class using the base keyword.
  • The override, virtual, and new keywords can also be applied to properties, indexers, and events.

Virtual:

The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in

a derived class

You cannot use the virtual modifier with the static, abstract, private or override modifiers.

Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.

  • It is an error to use the virtual modifier on a static property.
  • A virtual inherited property can be overridden in a derived class by including a property declaration that uses the
  • Override

 

*******************************************************************************************************************

Example 1:

// Define the base class

class Car

{

public virtual void DescribeCar()

{

System.Console.WriteLine(“Four wheels and an engine.”);

}

}

 

// Define the derived classes

class ConvertibleCar : Car

{

public new virtual void DescribeCar()

{

base.DescribeCar();

System.Console.WriteLine(“A roof that opens up.”);

}

}

 

class Minivan : Car

{

public override void DescribeCar()

{

base.DescribeCar();

System.Console.WriteLine(“Carries seven people.”);

}

}

public static void TestCars1()

{

Car car1 = new Car();

car1.DescribeCar();

System.Console.WriteLine(“———-“);

 

ConvertibleCar car2 = new ConvertibleCar();

car2.DescribeCar();

System.Console.WriteLine(“———-“);

 

Minivan car3 = new Minivan();

car3.DescribeCar();

System.Console.WriteLine(“———-“);

}

output:

Four wheels and an engine.

———-

Four wheels and an engine.

A roof that opens up.

———-

Four wheels and an engine.

Carries seven people.

———-

Example 2:

public static void TestCars2()

{

Car[] cars = new Car[3];

cars[0] = new Car();

cars[1] = new ConvertibleCar();

cars[2] = new Minivan();

 

 

foreach (Car vehicle in cars)

{

System.Console.WriteLine(“Car object: ” + vehicle.GetType());

vehicle.DescribeCar();

System.Console.WriteLine(“———-“);

}

 

}

output:

Car object: YourApplication.Car

Four wheels and an engine.

——————————————-

Car object: YourApplication.ConvertibleCar

Four wheels and an engine.

——————————————–

Car object: YourApplication.Minivan

Four wheels and an engine.

Carries seven people.

———-

note:As the new keyword was used to define this method, the derived class method is not called—the base class method is called instead. The Minivan object correctly calls the overridden method, producing the results we expected.

*******************************************************************************************************************

 

 

Abstract class

 

  • A class which contains partially implemented method and concrete methods.
  • An abstract class cannot be instantiated (means we can’t create instance).
  • Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block.
  • Derived classes of the abstract class must implement all abstract methods.
  • The abstract method cannot be marked virtual.
  • We can call abstract methods in concrete methods of the abstract class bcoz we can’t create instance to that class.

*******************************************************************************************************************

Example 1:

using System;

public abstract class BClass

{

 

public abstract void abstractmethod();

public void bmethod()

{

Console.WriteLine(“This is base class method”);

abstractmethod();

}

}

public class ChClass1:BClass

{

public void ch1method()

{

Console.WriteLine(“This is child class1  method”);

 

}

public override void abstractmethod()

{

 

Console.WriteLine(“This is child1 class+base class override method”);

}

 

}

 

public class sample

{

public static void Main()

{

ChClass1 ch1=new ChClass1();

 

//ch1.abstractmethod();

ch1.bmethod();

 

Console.WriteLine(“————————————“);

 

}

 

}

*******************************************************************************************************************

Property

Property – A property is a thing that describes the features of an object

Event

Event – An action that an object does. When something happens, we say an event has happened. For example, when a button is clicked, we say it is the click( ) event. When a mouse hovers on an image, we say the mouseover( ) event has taken place

 

Access Modifiers:

 

The main purpose of using access specifies is to provide security to the applications

Public: It can be accessed from anywhere in the class and outside the class

Private: It can be accessed with in the class only (only by the members of the class. it can’t be accessed outside the class

Internal: It can be accessed with in the application (by all classes within an assembly) but not from outside the assembly

Protected: It can be accessed within the class as well as the classes that inherits this class

Protected internal:It can be accessed within the application and inherited classes.

 

******Private: Private class variable or methods can be inherited in child class but we can’t access those variables

 

Static:

 

The main features of a static class are:

  • They only contain static members.
  • They cannot be instantiated.
  • They are sealed.
  • They cannot contain Instance Constructors (C# Programming Guide).
  • Static classes must derive from objects.

NOTE: Static Modifier can be used for the class if and only if you are having C# 2.0 ie Vs.net 2005.

It cannot be done in VS.net 2003.

 

To work with members of static classes, no need to create their instances.

Static Member – A class member declared with the keyword static is a static member.

A static member is owned by the class name, not by its instances (objects of the class).

***Note that static members are actually class members, while non-static members are instance members (means they are owned by the instances). Both in C# & VB.NET, we may create static/shared events, properties, fields and functions. Note Indexers in C# cannot be declared static.

***Note Static member functions cannot access non-static members directly.

*** base keyword is not used in a static method

          Static Members: It shares the single data to all the instance of that class.

*******************************************************************************************************************

Example 1:

using System;

public static class StaticClass

{

public static void DoWork() {

++CallCount;

Console.WriteLine( “StaticClass.DoWork()” );

}

 

public class NestedClass {

public NestedClass() {

Console.WriteLine( “NestedClass.NestedClass()” );

}

}

 

public static long CallCount = 0;

}

 

public static class MainClass

{

static void Main() {

StaticClass.DoWork();

 

StaticClass.NestedClass nested = new StaticClass.NestedClass();

 

Console.WriteLine( “CallCount = {0}”, StaticClass.CallCount );

}

}

 

 

***************************************************************************************************************

Constructors

These are used to create and initialized the instance of the object

Constructor name and classname should be same

Private constructors are commonly used in classes that contain only static members. This helps to prevent the creation of an instance of a class by other classes when there are no instance fields or methods in that class.

***************************************************************************************************************

Example 1:

 

using System;

public class Baseclass

{

Baseclass()

{

Console.WriteLine(“Base class constuctor intialization”);

}

 

public static void Main()

{

Baseclass b=new Baseclass();

}

}

output:

Base class constuctor intialization

***************************************************************************************************************

*****default accessspecifier of the constructor is private so we can’t create instance to the private constructor in outside class. It only create instance in subclasses of same class and same class it self

***************************************************************************************************************

Example 2:

using System;

public class Baseclass

{

Baseclass()

{

Console.WriteLine(“Base class constuctor intialization”);

}

 

 

}

public class sample

{

public static void Main()

{

Baseclass b=new Baseclass();

}

}

 

Error:

error CS0122: ‘Baseclass.Baseclass()’ is inaccessible due to its protection level

*******************************************************************************************************************************

***We can’t inherit the class that contain private constructor

***************************************************************************************************************Example 3:

using System;

public class Baseclass

{

Baseclass()

{

Console.WriteLine(“Base class constuctor intialization”);

}

}

public class Childclass:Baseclass

{}

public class sample

{

public static void Main()

{

Childclass b=new Childclass();

}

}

error CS0122: ‘Baseclass.Baseclass()’ is inaccessible due to its protection level

***************************************************************************************************************

 

Static constructor

  • A static constructor is used to initialize a class. It is not used to initialize instance of a class.
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method

 

 

***************************************************************************************************************

Example 1:

using System;

public class Baseclass

{

public Baseclass()

{

Console.WriteLine(“Base class constuctor intialization”);

}

 

public void bmethod()

{

Console.WriteLine(” base class method “);

}}

public class Childclass:Baseclass

{

public Childclass ()

{

Console.WriteLine(“child class constuctor intialization”);

}

}

public class sample

{

public static void Main()

{

Childclass b=new Childclass();

}

}

 

output:

Base class constuctor intialization

child class constuctor initialization

 

Example 2:

using System;

public class Baseclass

{

static Baseclass()

{

Console.WriteLine(“Base class constuctor intialization”);

}

 

public void bmethod()

{

Console.WriteLine(” base class method “);

 

}

}

public class Childclass:Baseclass

{

static Childclass ()

{

Console.WriteLine(“child class constuctor intialization”);

}

}

public class sample

{

public static void Main()

{

Childclass b=new Childclass();

}

}

output:

child class constuctor intialization

Base class constuctor initialization

 

Example3:

using System;

class MyClass

{

public static int x = 20;

public static int y=100;

public static int z = 25;

 

static MyClass () //normal constructor

{

Console.WriteLine (“static {0}”, y);

}

}

public class OutsideClass

{

public static void Main ()

{

Console.WriteLine (MyClass.x);

Console.WriteLine (“————————-“);

MyClass c=new MyClass ();

Console.WriteLine (“————————-“);

}

}

Output:

Static 100

20

———————

—————–

Example4:

using System;

class MyClass

{

public static int x = 20;

public static int y=100;

public static int z = 25;

 

public MyClass () //normal constructor

{

Console.WriteLine (“static {0}”, y);

}

}

public class OutsideClass

{

public static void Main ()

{

Console.WriteLine (MyClass.x);

Console.WriteLine (“————————-“);

MyClass c=new MyClass ();

Console.WriteLine (“————————-“);

}

}

Output:

20

———————

Static 100

—————–

***************************************************************************************************************

Interfaces

An interface has the following properties:

  • An interface is like an abstract base class: any non-abstract type inheriting the interface must implement all

its members.

  • An interface cannot be instantiated directly.
  • Interfaces can contain events, indexers, methods and properties.
  • Interfaces contain no implementation of methods.
  • Classes and structs can inherit from more than one interface.
  • An interface can itself inherit from multiple interfaces.

***************************************************************************************************************

Example 1:

using System;

interface Ibase

{

void test();    //Don’t use access specifiers

}

public class childclass:Ibase

{

public  void test()   //override method access specifier  should be either static nor public only

{

Console.WriteLine(“this is child interface inheritance”);

 

}

}

public class OutsideClass

{

public static void Main ()

{

childclass c=new childclass();

c.test();

}

}

Output:

this is child interface inheritance

***************************************************************************************************************

Generic class

 

Generic classes encapsulate operations that are not specific to a particular data type.

 

  • Use generic types to maximize code reuse, type safety, and performance.
  • The most common use of generics is to create collection classes.
  • The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. These should be used whenever possible instead of classes such as Array List in the System. Collections namespace.
  • You can create your own generic interfaces, classes, methods, events and delegates.
  • Generic classes may be constrained to enable access to methods on particular data types.
  • Information on the types that are used in a generic data type may be obtained at run-time by using reflection.

 

 

Partial Classes

“It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled”. The partial types for a specific type Should be located in the same module or assembly.

 

  • ***Types are defined using the keyword “partial”
  • ***The partial keyword applies to classes, strut, and interfaces, but not enums.

Following are the major benefits of using partial classes:

  • Isolation of the business logic of an application from its user interface
  • Facilitates easier debugging

Implementing Partial Classes

We can split one part of a class in one file and the other part in some other file using the partial keyword. The compiler merges these partial classes spread across one or more source files into a single compiled class at the time of compilation — provided all these partial types are available. The following code examples illustrate this better:

***************************************************************************************************************

Employee1.cs

public partial class Employee

{

public void ReadData()

{

//Some code

}

}

 

Employee2.cs

public partial class Employee

{

public void SaveData()

{

//Some code

}

}

 

class Test

{

public static void Main(string[] args)

{

Employee employee = new Employee();

//Some Code

employee.ReadData();

//Some code

employee.SaveData();

}

}

 

***************************************************************************************************************

Value Types: when we pass the variable of an explicit data type to a method if we change the value type of that variable in a method, its value of the variable would not be change outside of the method. (actual parameters and formal parameters)

Reference Types: objects are implicitly passed by reference. If we modify the object in a method, its value would be changed outside of the method.

Boxing and Unboxing: 

Boxing permits any value type to be implicitly converted to type object or to any interface type implemented by value type. Boxing is process in which a object instances created and copying value types value in to that instance. Unboxing is vice versa of boxing operation where the value is copied from the instance in to appropriate storage location.

    ***************************************************************************************************************

 

        Example:

        object myObject = 3;
int myInt3 = (int) myObject;  // myObject is unboxed
Console.WriteLine(“myInt3 = ” + myInt3);

int x;

object obj;

x = 10;

obj = x; // box x into an object

int y = (int)obj; // unbox obj into an int

Console.WriteLine(y);

   ***************************************************************************************************************

 

Late Binding and DelayBinidng:

Memory will be allocated while instance is creating but in the case of late binding memory will be allocated after the statement is created

***************************************************************************************************************

Baseclass b=new BaseClass (); early binding

Baseclass c;

C=new Baseclass () late binding

***************************************************************************************************************

    

 

     String and StingBuilder

  • String is immutable .stringbulider is mutable
  • Internal state of the string object doesn’t change by its operations, In the case of stringbuilder its value changed by its operations.
  • Namespace: System.Text, String Builder

System.String

  • For stringbuilder we need to call the constructor to initiate the object

 

Categories: Uncategorized

HCL Asp.Net Interview

Categories: Uncategorized

Visual Studio Remove Items from the Recent Projects List

Categories: Uncategorized

K2 Demos

Categories: Uncategorized