Defining the class definitions in individual files

 All of the examples up to now have been implemented with one or more classes within the same file.
 But in the real programming world, each class will be implemented in individual file.
 To do so,
o Click on “Project” menu – “Add Class”.
o Enter the name of the new class.
o Click on “Add” button to confirm.
o Then a new file will be created in the same project named as: classname.cs

Note: (cs stands for C Sharp)

Class Definition in individual files

Demo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassDefInSeperateFiles
{
class Demo
{
public void DemoMethod()
{
Console.WriteLine("This is demo method...");
}
}
}
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassDefInSeperateFiles
{
class Program
{
static void Main(string[] args)
{
Demo d = new Demo();
d.DemoMethod();
Console.Read();
}
}
}
Partial Classes

Partial Classes

 The “partial” keyword allows you to define a class, struct or interface across multiple files.
 As you learned above, each class will be placed in individual files.
 But in some situations in future, you may require to define a class within multiple files. In other words, the class definition can be split into multiple files.
 For example, if multiple programmers need to develop a class simultaneously, this may be required. In the same way, in windows applications, the automatic code generator generates some design part code for your own form class. In this case also, “partial classes” are required.

Implementation:

MyClassPart1.cs

partial class myclass
{
//some members
}

MyClassPart2.cs

partial class myclass
{
}

//some more members

Partial Classes

SampleClassPart1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PartialClasses
{
partial class SampleClass
{
public void FirstMethod()
{
Console.WriteLine("This is first method.");
}
}
}

SampleClassPart2.cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PartialClasses
{
partial class SampleClass
{
public void SecondMethod()
{
Console.WriteLine("This is second method.");
}
}
}

SampleClassPart3.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PartialClasses
{
partial class SampleClass
{
public void ThirdMethod()
{
Console.WriteLine("This is third method.");
}
}
}

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PartialClasses
{
class Program
{
static void Main(string[] args)
{
SampleClass sc = new SampleClass();
sc.FirstMethod();
sc.SecondMethod();
sc.ThirdMethod();
Console.Read();
}
}
}

Partial Classes

Static Classes

 A static class can be created using a keyword called “static” used at the class definition.
 A static class can contain only static members (static data members and static methods).
 You can’t create an object for the static class.

 Advantages:
 If you declare any non-static member, it generates a compile time error; so that it is guaranteed that the class contains only static members; no chance of declaring non-static member accidentally.
 When you try to create an instance to the static class, it again generates a compile time error, because the static members can be accessed directly with its class name.

 Syntax:
static class classname
{
//static data members
//static methods

Static Classes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StaticDemo
{
static class Sample
{
public static string SampleStaticMember = "This is my static data member";
public static void SampleStaticMethod()
{
Console.WriteLine("This is my static method.");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Sample.SampleStaticMember);
Sample.SampleStaticMethod();
Console.Read();
}
}
}
Partial Classes