Q:

What are partial classes?

0

What are partial classes?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

A partial class is only used to split the definition of a class in two or more classes in the same source code file or more than one source file. You can create a class definition in multiple files, but it will be compiled as one class at run time. Also, when you create an instance of this class, you can access all the methods from all source files with the same object.
 

Partial Classes can be created in the same namespace. It isn’t possible to create a partial class in a different namespace. So use the “partial” keyword with all the class names that you want to bind together with the same name of a class in the same namespace.

 
Syntax:
public partial Clas_name  
{
        // code
}

Let’s see an example:

// C# program to illustrate the problems 
// with public and private members 
using System;
public partial class Coords
{
    private int x;
    private int y;
    public Coords(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
public partial class Coords
{
    public void PrintCoords()
    {
        Console.WriteLine("Coords: {0},{1}", x, y);
    }
}
class TestCoords
{
    static void Main()
    {
        Coords myCoords = new Coords(6, 27);
        myCoords.PrintCoords();
        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

Output:

Coords: 10,15
Press any key to exit.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Interview Questions and Answers,You Need To Know

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What’s the difference between the System.Array.C... >>
<< What is the difference between the dispose and fin...