1. So these are - Private, Protected, Internal, ProtectedInternal and Public.
2. Class/Struct can have Internal(default)/Public.
3. Struct members (nested class & structs etc) can have - Private(default), Internal and Public.
4. Class members (nested class & structs etc) can have - Private(default), Protected, Internal, ProtectedInternal and Public.
5. Default Access modifier for class constructor is - protected.
6. Friend assemblies - InternalsVisibleToAttribute.
7. Derived classes cannot have greater accessibility than their base types. In other words, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.
8. Destructors cannot have accessibility modifiers.
Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts
Tuesday, 15 November 2011
Private nested class
Why?
1. Framework, implements IEnumerable pattern as nested class. Of course these type of class has no other purpose other than parent class needs.
3. to continue:):):)
1. Framework, implements IEnumerable pattern as nested class. Of course these type of class has no other purpose other than parent class needs.
class MyUselessList : IEnumerable {
// ...
private List internalList;
private class UselessListEnumerator : IEnumerator {
private MyUselessList obj;
public UselessListEnumerator(MyUselessList o) {
obj = o;
}
private int currentIndex = -1;
public int Current {
get { return obj.internalList[currentIndex]; }
}
public bool MoveNext() {
return ++currentIndex < obj.internalList.Count;
}
}
public IEnumerator GetEnumerator() {
return new UselessListEnumerator(this);
}
}
2. A fully lazy implementation for Singleton pattern(http://suresh-anothernetprogrammer.blogspot.com/2011/11/singleton.html)3. to continue:):):)
Thursday, 10 November 2011
Idea1 - Avoid Switch/If conditions
While developing a web service, i came across a scenario where i need to create a Factory pattern to instantiate a Response class based on the Request string.
For example, if the Request is 'AuthenticateUserRequest', i need to create 'AuthenticateUserResponse'.
So i started writing an switch case like below
switch(requestName)
{
case "AuthenticateUserRequest":
return new AuthenticateUserResponse();
}
soon the method grows big, and i wanted to avoid this.
so i declared an dictionary with key value pair, with key being the request name and value being the response being the corresponding response class.
so it becomes
Dictionary responses = new Dictionary
{
{"AuthenticateUserRequest", new AuthenticateUserResponse()},
{"CreateOrderRequest", new CreateOrderResponse() }
};
-----
return responses[requestName].value.Clone();
For example, if the Request is 'AuthenticateUserRequest', i need to create 'AuthenticateUserResponse'.
So i started writing an switch case like below
switch(requestName)
{
case "AuthenticateUserRequest":
return new AuthenticateUserResponse();
}
soon the method grows big, and i wanted to avoid this.
so i declared an dictionary with key value pair, with key being the request name and value being the response being the corresponding response class.
so it becomes
Dictionary
{
{"AuthenticateUserRequest", new AuthenticateUserResponse()},
{"CreateOrderRequest", new CreateOrderResponse() }
};
-----
return responses[requestName].value.Clone();
Tuesday, 8 November 2011
Singleton Pattern
What is beforefieldinit?
1. All classes with static constructors will NOT be marked as 'beforefieldinit'
2. When a class is marked as 'beforefieldinit', then type initialization may be eager or lazy. So what i mean by this is
EAGER
using System;
class Test
{
public static string x = EchoAndReturn ("In type initializer");
public static string EchoAndReturn (string s)
{
Console.WriteLine (s);
return s;
}
}
class Driver
{
public static void Main()
{
Console.WriteLine("Starting Main");
// Invoke a static method on Test
Test.EchoAndReturn("Echo!");
Console.WriteLine("After echo");
// Reference a static field in Test
string y = Test.x;
// Use the value just to avoid compiler cleverness
if (y != null)
{
Console.WriteLine("After field access");
}
}
}
OUTPUT
In type initializer
Starting Main
Echo!
After echo
After field access
--------------------------------------------------
LAZY
Starting Main
Echo!
After echo
In type initializer
After field access
3. If the class 'Test' have static constructor, then only below output is possible
Main
In type initializer
Echo!
After echo
After field access
So what we understand from above points, when we have static constructor - then the type initializers called when instance/static members or functions are called on that type. If not, we can't guarantee, as type initializer might be eager or lazy.
---------------------------------------------------------------------------------
So 'readonly' fields can be initialized while declaring or in constructor, so 'static readonly' are initialized during declaration or static constructor. it also tells, reference can't be changed - means only one reference.
So
public sealed class Singleton
{
public static readonly Singleton instance = new Singleton();
static Singleton()
{
}
Singleton()
{
}
}
and
public sealed class Singleton
{
public static readonly Singleton instance;
static Singleton()
{
instance = new Singleton();
}
Singleton()
{
}
}
------------------------------------------------------
and full lazy initialization
public sealed class Singleton
{
private Singleton()
{
}
public static Singleton Instance
{
get
{
return NestedSingleton._instance;
}
}
private class NestedSingleton
{
static NestedSingleton()
{ }
public static readonly Singleton _instance = new Singleton();
}
}
So why fully lazy initialization?
because static constructor called when static fields are initializes(bit uncontrolled), so for expensive constructor operation + better control, lazy loading is perfect
1. All classes with static constructors will NOT be marked as 'beforefieldinit'
2. When a class is marked as 'beforefieldinit', then type initialization may be eager or lazy. So what i mean by this is
EAGER
using System;
class Test
{
public static string x = EchoAndReturn ("In type initializer");
public static string EchoAndReturn (string s)
{
Console.WriteLine (s);
return s;
}
}
class Driver
{
public static void Main()
{
Console.WriteLine("Starting Main");
// Invoke a static method on Test
Test.EchoAndReturn("Echo!");
Console.WriteLine("After echo");
// Reference a static field in Test
string y = Test.x;
// Use the value just to avoid compiler cleverness
if (y != null)
{
Console.WriteLine("After field access");
}
}
}
OUTPUT
In type initializer
Starting Main
Echo!
After echo
After field access
--------------------------------------------------
LAZY
Starting Main
Echo!
After echo
In type initializer
After field access
3. If the class 'Test' have static constructor, then only below output is possible
Main
In type initializer
Echo!
After echo
After field access
So what we understand from above points, when we have static constructor - then the type initializers called when instance/static members or functions are called on that type. If not, we can't guarantee, as type initializer might be eager or lazy.
---------------------------------------------------------------------------------
So 'readonly' fields can be initialized while declaring or in constructor, so 'static readonly' are initialized during declaration or static constructor. it also tells, reference can't be changed - means only one reference.
So
public sealed class Singleton
{
public static readonly Singleton instance = new Singleton();
static Singleton()
{
}
Singleton()
{
}
}
and
public sealed class Singleton
{
public static readonly Singleton instance;
static Singleton()
{
instance = new Singleton();
}
Singleton()
{
}
}
------------------------------------------------------
and full lazy initialization
public sealed class Singleton
{
private Singleton()
{
}
public static Singleton Instance
{
get
{
return NestedSingleton._instance;
}
}
private class NestedSingleton
{
static NestedSingleton()
{ }
public static readonly Singleton _instance = new Singleton();
}
}
So why fully lazy initialization?
because static constructor called when static fields are initializes(bit uncontrolled), so for expensive constructor operation + better control, lazy loading is perfect
Subscribe to:
Posts (Atom)