How to implement partial classes using console application?


In this article,partial classes are implemented using console application. Partial classes are easy to use and maintain. Generic types can also be partial. I hope this article will be useful for all beginners of .Net

How to implement partial classes using console application?
Description - Partial classes are a way of splitting the source code which defines a class into separate files so that multiple developers can work at the same time.
This way class definition section is present in the each source file.
Steps -
1) Add console application and add two class files in it which contains partial classes.
Add following code in PartialClassDivide.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
partial class PartialClassCaluclation
{
public float divide(int number1, int number2)
{
if (number2 != 0)
{
return (number1 / number2);
}
else if (number1 != 0)
{
return (number2 / number1);
}
else
{
return 0;
}
}
}
}

2) Add following code in PartialClassSubtraction.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
partial class PartialClassCaluclation
{
public int Subtract(int number1, int number2)
{
return (number1 - number2 );
}
}
}

3)Add following code in Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
PartialClassCaluclation obj = new PartialClassCaluclation();
Console.WriteLine("Divide ans:" + obj.divide(50, 5));
Console.WriteLine("Subtraction ans:" + obj.Subtract(112, 14));
}
}
}

I have created object of PartialClassCaluclation and used both methods implemented in two different classes. As those are partial classes, I could use those methods with single object.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: