Value and Reference Types

Value type is a data type which are either built-in types, or structure(struct) or an enumeration(enum).When value-type instance are created, only a single space in memory is allocated to store the value.

Reference type are referred to as objects including Classes, Interfaces, Delegates, Instances arrays, and as well as the built in reference types. When reference types is created two spaces are allocated one for instance and another for its reference.

The runtime deals with the value types and reference types in two different ways.

theStruct myStruct1 = new theStruct();
myStruct1.X = 10;

//Struct is a value type,
//myStruct2 becomes an independent copy of myStruct1, with its own fields.
theStruct myStruct2 = myStruct1;
myStruct2.X = 20;
Console.WriteLine("myStruct1: {0}", myStruct1.X);
Console.WriteLine("myStruct2: {0}", myStruct2.X);

Output
 
myStruct1:10
myStruct2:20

Now Repeat the same with a reference type.

The output will be 20,20.

Singleton Design Pattern

Singleton design pattern allows to create only one instance of the class by providing only one global point to access it.
This can be achieved by:

    1. Making the constructor private/protected.
    2. Create a static function which returns an instance of the class only after ensuring class has no other instance.


namespace OopConcept
{
///
/// Class without explicit contructor
///

class singleton
{
private static singleton singletonInstance = null;

///
/// To prevent instantiation.
///

private singleton()
{

}
public static singleton getInstance()
{
if (singletonInstance == null)
{
singletonInstance = new singleton();
}
return singletonInstance;
}

}
///
/// Main Class -
///

class MainClass
{
public static void Main()
{
// Cannot create an instance with 'new' Keyword.
singleton firstInstance = singleton.getInstance();
singleton secondInstance = singleton.getInstance();

if (firstInstance == secondInstance)
{
Console.WriteLine("Two instances are same");
Console.Read();
}
}
}
}

Constructors in C#.

C# creates default constructor for all classes. We do not need to create a constructor explicitly. There will be a default constructor.


namespace OopConcept
{
///
/// Class without explicit contructor
///

class mainClass
{
private int m_int;

static void Main(string[] args)
{
//*******>No error*******
mainClass myMainClass = new mainClass();
}
}
}


But when we create a parametrized constructor the default constructor will be removed..


class mainClass
{
private int m_int;

///
/// Parametrized contructor created by the user.
///

///

public mainClass(int num)
{
this.m_int = num;
}
static void Main(string[] args)
{
//*******ERROR********
mainClass myMainClass = new mainClass();
}
}

WPF Styles..

Allow the user to create visually compelling affects and by customizing the appearance.

Some features of styles are :

  • Style exists only within one WPF app. - cannot be applied to anything outside of the WPF application in which they exist, nor can they be applied to any WinForms controls hosted within a WPF Window.

  • separation of presentation and logic. This means that designers can work on the appearance of an application by using only XAML at the same time that developers work on the programming logic by using C# or Visual Basic.


Let see an example without style.


<StackPanel>
<Label Background="YellowGreen"
FontStyle="Normal"
Padding="8,4"
Margin="4"
Height="38"
Width="68">Without</Label>
<Label Background="YellowGreen"
FontStyle="Normal"
Padding="8,4"
Margin="4"
Width="70"
Height="34">Style</Label>
</StackPanel>

WPF User Control....

A Simple program using User Control in WPF.

Reuse the userControl which takes the user name and password.

1. Create new project in WPF(I named the project UserControlSample).

2. Add a new user control to that project (Right Click and Add -> User Control). Since my user control takes user name and password I renamed my user control as RegisterUserControl.

3. Add two labels two text boxes RegisterUserControl.

RegisterUserControl.xaml


<Window x:Class="UserControlSample.RegisterWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UserControlSample"
Title="RegisterWindow"
Height="400"
Width="400"
Loaded="
Window_Loaded">
<Grid>
<local:RegisterUserControl
Margin="0,-21,40,21"
Height="268">
</local:RegisterUserControl>
<Button Content="Button"
Height="23"
HorizontalAlignment="Left"
Margin="208,256,0,0"
Name="Login"
VerticalAlignment="Top"
Width="75" />
</Grid>
</Window>


The RegistrationWindow uses the RegisterUserControl to add User name and password to it.

RegisterWindow.xaml


<UserControl x:Class="UserControlSample.RegisterUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Label Content="UserName"
Height="28"
HorizontalAlignment="Left"
Margin="28,63,0,0"
Name="label1"
VerticalAlignment="Top"
Width="77" />
<Label Content="Password"
Height="28"
HorizontalAlignment="Left"
Margin="28,112,0,0"
Name="label2"
VerticalAlignment="Top"
Width="77" />
<TextBox Height="28"
HorizontalAlignment="Left"
Margin="140,63,0,0"
Name="UsernameTextBox"
VerticalAlignment="Top"
Width="120" />
<TextBox Height="28"
HorizontalAlignment="Left"
Margin="140,112,0,0"
Name="PasswordTextBox"
VerticalAlignment="Top"
Width="120" />
</Grid>
</UserControl>


I also added a login button to our main RegisterWindow.