Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
What is Wrapper class in dot net?
Posted
Comments
johannesnestler 3-Sep-13 9:12am    
Hmm - how does a wrapper class in .NET differs from other wrapper classes in other OOP languages/Frameworks? I think you should better ask what is a wrapper class in OOP? This is nothing specific to .NET - and if I think about it, I can't find a better name for code wrapping calls/data to other code than - WRAPPER (but my native language is not english, so maybe I miss the other "Options" ;-)

A Wrapper class is one that "encapsulates" a resource, or another class in order to simplify or restrict the interface between the outside world and the encapsulated object. For example, a wrapper class might encapsulate a COM object, providing .NET access methods and properties and translating them internally to the function calls to the actual COM interface. Or it might "hide" a full Dictionary object, allowing you to just add, query and remove items from it, without knowing what the underlying storage is.

If you want to compare it to the real world, the "drive a car" class wraps the complexity of the engine by providing an Accelerator object with "press" and "release" methods that hide the complexity of the fuel injection mapping from the user.
 
Share this answer
 
Comments
Menon Santosh 5-Sep-13 13:35pm    
well said my +5
joginder-banger 24-Jul-16 7:51am    
agree with you sir +5
A wrapper class is a class that encapsulates and hides aspects of another class/resource that would only add confusion or risk of misuse. They can also be used to wrap functionality exposed using a different paradigm so that it is exposed using, for example, in an object orient approach (like wrapping up calls to a native Dll using DllImport in a call that looks and feels like a "normal" C# class but maintains and controls all the calls to the native functions.

Hope this helps,
Fredrik
 
Share this answer
 
hi,
Wrapper Class
A wrapper or container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In Apex and Visualforce this type of class can be extremely useful but to a new developer the talk of wrapper classes may fly right over your head, it definitely did for me. Here I will supply a simple demo of how wrapper classes can be used.

example
C#
/// Example for Wrapper class

public class Wrapper
{
    private int x = 10;
    private int y = 20;

    
    public Wrapper()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public int add(int x, int y)
    {
        Wrapper.inside obj = new inside();
        //core functionality of this method is written in the wrapped class.
        int sum = obj.add(x, y);
        return sum;
    }

    /// 

    /// this class is wrapped by the wrapper class. 
    /// we can access only inside this class.
    /// outside the class, we cant access this class.
    /// 

    private class inside
    {        

        public int add(int x, int y)
        {
           
            return x + y;
        } 
    }
}


Happy Coding :)
 
Share this answer
 
Comments
johannesnestler 3-Sep-13 9:16am    
My Vote of 1. Sorry, a well written answer but I think you messed up "container" with "wrapper" - These are completely separate things. Your answer talks about container classes, your example code Shows a Kind of a wrapper - but in a quite unrealistic Scenario. So downvote is just for not really answering OP's question, and not for the quality or the content of your writing.
Priyanka Bhagwat 3-Sep-13 9:32am    
Hey,
Thankyou for your reply..
I learnt something new....:)
Priyanka Bhagwat 3-Sep-13 9:33am    
I misunderstood it..I thought that the wrapper and container are same
Member 14148694 21-Aug-19 2:16am    
best example ,, thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900