Private Class C

Private Class C




🛑 ALL INFORMATION CLICK HERE 👈🏻👈🏻👈🏻

































Private Class C






Downloads



Visual Studio



SDKs



Trial software


Free downloads


Office resources






Programs



Subscriptions


Overview


Administrators





Students


Microsoft Imagine


Microsoft Student Partners





ISV



Startups



Events




Community



Magazine



Forums



Blogs



Channel 9




Documentation



APIs and reference



Dev centers



Samples



Retired content








We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second.



Marked as answer by
Fafafaalex Moderator
Friday, July 16, 2010 5:41 AM






Marked as answer by
Fafafaalex Moderator
Friday, July 16, 2010 5:42 AM



public class Singleton {
private Singleton() {
}
private static Singleton _singleInstance;
static Singleton() {
_singleInstance= new Singleton();
}

public static Singleton Instance {
get {
return _singleInstance;
}
}

}




Marked as answer by
Fafafaalex Moderator
Friday, July 16, 2010 5:41 AM






Marked as answer by
Fafafaalex Moderator
Friday, July 16, 2010 5:41 AM






Marked as answer by
Fafafaalex Moderator
Friday, July 16, 2010 5:41 AM



public class Singleton {
private Singleton() {
}
private static Singleton _singleInstance;
static Singleton() {
_singleInstance= new Singleton();
}

public static Singleton Instance {
get {
return _singleInstance;
}
}

}




Marked as answer by
Fafafaalex Moderator
Friday, July 16, 2010 5:41 AM






Marked as answer by
Fafafaalex Moderator
Friday, July 16, 2010 5:41 AM






Marked as answer by
Fafafaalex Moderator
Friday, July 16, 2010 5:42 AM






Marked as answer by
Fafafaalex Moderator
Friday, July 16, 2010 5:41 AM






Marked as answer by
Fafafaalex Moderator
Friday, July 16, 2010 5:41 AM



A private class can be useful in ANY class, not just in another private class. You use it to encapsulate things in a way that does not need to be exposed publically.
Namespace Test
{
private class TestClass
{
}
}

namespace Test
{
public class TestClass
{
private class InternalTest
{
}

private InternalTest internalStuff;

public TestClass()
{
internalStuff = new InternalTest();
}
}
}





Edited by
Serge1966
Friday, October 1, 2010 6:10 PM






Proposed as answer by
Serge1966
Monday, October 4, 2010 8:04 PM






Proposed as answer by
Rudedog2 Moderator
Friday, October 1, 2010 3:44 PM




Dev centers


Windows


Office


Visual Studio


Microsoft Azure


More...




Learning resources


Microsoft Virtual Academy


Channel 9


MSDN Magazine




Community


Forums


Blogs


Codeplex




Programs


BizSpark (for startups)


Microsoft Imagine (for students)






Newsletter


Privacy & cookies


Terms of use


Trademarks



  I have a question which I would like to ask my friends (.net friends) who think .NET .
  Why should we use a private class. How can someone create a class which is private i.e (has private constructor) and a private access modifier. What is the use of a class which is private and cannot be called from other classes.
            We can declare a class as Private inside other class. kindly find the code below on how to achieve the same:    
They can be used for internal use within the parent class.
Are we talking of: private class or private constructor?
This is a class, which can not be instantiated. There is only one object present, which can be accessed by Singleton.Instance
Private class can provide an public interface... If you don't want to show the class, but you want to have the interface implementation.
Mostly c# devlopers use private class(i.e private constructor) to make singletone classes(pattern). singletone means
"A singleton is a single-instance object, and they simplify complex code. Singletons have a static property that you must access to get the object reference."
For more information on singleton pattern u can visit below link:
Also, The reason you want to use private is because sometimes you want your class to hide information from the rest of the program. It's the whole idea behind
encapsulation.
For more detail regarding this aspect u can visit :
May be it helps u to understand private keyword/class.
If anything unclear feel free to ask me :)
  
Welcome to MSDN forums! I'm glad to see your active participation and discussion in MSDN forums. Based on my understanding The private class data design pattern seeks to reduce exposure of attributes by limiting their visibility. It reduces the
number of class attributes by encapsulating them in single Data object. It allows the class designer to remove write privilege of attributes that are intended to be set only during construction, even from methods of the target class.

   
private static
WorldCup instance;
   
public static
WorldCup GetWorldCup() {
       
if (instance == null ) instance =
new WorldCup ();
WorldCup only one in the world.  Privarte constructor can limit create it's instance.
  If you still have any doubt and concern about this issue, please let me know. If I misunderstood you, please kindly elaborate your question.
Please Mark as Answered
If this is helpful Or Un-Mark as Answered if it is not helpful.
Best Regards,
Yan Jun
Microsoft Online Community Support
Are we talking of: private class or private constructor?
This is a class, which can not be instantiated. There is only one object present, which can be accessed by Singleton.Instance
Private class can provide an public interface... If you don't want to show the class, but you want to have the interface implementation.
            We can declare a class as Private inside other class. kindly find the code below on how to achieve the same:    
They can be used for internal use within the parent class.
Mostly c# devlopers use private class(i.e private constructor) to make singletone classes(pattern). singletone means
"A singleton is a single-instance object, and they simplify complex code. Singletons have a static property that you must access to get the object reference."
For more information on singleton pattern u can visit below link:
Also, The reason you want to use private is because sometimes you want your class to hide information from the rest of the program. It's the whole idea behind
encapsulation.
For more detail regarding this aspect u can visit :
May be it helps u to understand private keyword/class.
If anything unclear feel free to ask me :)
Private constructors is not allowed. They have to be public.
Hmm ... what do I miss? Private constructors are allowed and usefull.
One example can be the Skeleton Pattern where you get an instance - but always the same so only one object of the class exist.
Or I just used it when I wanted to simplify a class which only had a public static function "Render(.....)" that was creating a XPS Document. I had a lot of arguments passing to each (private) function so I quickly changed from
a static class to a class with a private constructor and the Render Function creates an instance and all stuff that I was giving to a lot of functions is now a property. (Ok, this could have been solved with an embedded class like you showed
in your example but I like the design with the private Constructor much more!)
  
Welcome to MSDN forums! I'm glad to see your active participation and discussion in MSDN forums. Based on my understanding The private class data design pattern seeks to reduce exposure of attributes by limiting their visibility. It reduces the
number of class attributes by encapsulating them in single Data object. It allows the class designer to remove write privilege of attributes that are intended to be set only during construction, even from methods of the target class.

   
private static
WorldCup instance;
   
public static
WorldCup GetWorldCup() {
       
if (instance == null ) instance =
new WorldCup ();
WorldCup only one in the world.  Privarte constructor can limit create it's instance.
  If you still have any doubt and concern about this issue, please let me know. If I misunderstood you, please kindly elaborate your question.
Please Mark as Answered
If this is helpful Or Un-Mark as Answered if it is not helpful.
Best Regards,
Yan Jun
Microsoft Online Community Support
well guys thatnks a got for all the replies,
 I totally agree with you on the following points:
 1) Private Class can be used inside another private class. In this way private class call be called by the parent class (or create an instance of it)
2) Concept of private class (with private constructor) is also used to have a singleton pattern implementation in the code.
 But somehow I feel that there is more to it, I think private classes have more to give us. Can you guys tell me in which senario you would think Private class can be the only option.
 I totally agree with you on the following points:
 1) Private Class can be used inside another private class. In this way
private class call be called by the parent class (or create an instance of
it)
Yes, that is in my eyes the only use for a class that is marked as private.
2) Concept of private class (with private constructor) is also used to have
a singleton pattern implementation in the code.
But remember: The class itself is not private. There is no public constructor to make sure, that nobody "from outside" can create an instance of the class.
The most famous example for this is the singleton pattern. But I also used it in the past with a static class that created an instance on a complex call just to get an object on which I could work inside. (Which could have been solved
with an internal class for this.)
 But somehow I feel that there is more to it, I think private classes have
more to give us. Can you guys tell me in which senario you would think
Private class can be the only option.
I don't think that this has something to do with "only option". It has only to do with clean code. The singleton pattern could also be implemented with a public constructor. But then you have to check inside the constructor, that
the private field for the instance is null. If it is not null, then you raise an exception.
That way it is no real problem if someone uses the constructor directly. The documentation would tell the user not to use the constructor. But I hope that you agree: It is much better to mak sure, that you cannot run into this error.
So this public / private / internal / ... is more or less for clean code. That way you want to make sure, that others are not confused by to many options. So when you have a function in your class that does not make sense for others to
call from outside, then you mark it in a way that others do not see it (normaly it is public).
Of course: This has a much deeper impact. In Object Oriented Design, you want to hide the implementation from the outer world. So if you have a class that is used to store items, it should be possible to change the way how you store it
(That way you can rebuild it and replace your algorithm). That would not be possible if other people had access to the data directly (Either through a public property or a function).
And last but not least: It can make your code a little safer. One example could be code, that is using native calls to DLLs. You call them in a way that you can trust but you should make sure, that nobody else could call them through your
assembly. (Maybe someone trusts your assembly and that way it could be misused!)
I know this was a very short explanation but I hope it was possible to understand, what I wanted to say.
It is not a technical requirement to mark something as private, but there are good reasons to think about this topic carefully when writing code.
The simple questions are: Is this required from outside? Should "others" call / use it? (Where others are not only other people. Other classes / namespaces is also meant).
 Thanks for the answer , Yes I agree to your suggestion any many other's of using a private class (sorry private constructor) for the Singleton Pattern implementation , but Can you give me a senario where I can use a class which is marked as private
(not the constructor) and the use of such a class.
Well Why to have such a class , please explain with an example.....
 Thanks for the answer , Yes I agree to your suggestion any many other's of using a private class (sorry private constructor) for the S
Pron Nudist
Overwatch Origins Edition
Wife Double Penetration Gangbang

Report Page