The developer can then extend the code without disturbing the base code. Teams of developers collaborating on an application also benefit from partial classes. The class can be parceled based on responsibility. The developer works in separate source files, which isolate changes and focus a developer on code relevant to their responsibility.
Consistency is the key to partial types:
- All partial classes name must be same.
- Each partial type is preceded with the partial keyword.
- The partial types must have the same accessibility.
- If any part is sealed, the entire class is sealed.
- If any part is abstract, the entire class is abstract.
- Inheritance at any partial type applies to the entire class.
Step 1
Create a Console application and give the project name as a SolPartialClass.
Step 2
Create a partial classes. The parts of this class can be also defined in same file if needed.
public partial class PartialDemoClass { #region Method public void MethodA() { try { System.Console.WriteLine("Method A calling"); } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion } public partial class PartialDemoClass { #region Method public void MethodB() { try { System.Console.WriteLine("Method B Calling"); } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion }
Step 3
Create a instance of parital class in program class,it is look like this
class Program { static void Main(string[] args) { try { PartialDemoClass Obj = new PartialDemoClass(); Obj.MethodA(); Obj.MethodB(); } catch (Exception ex) { System.Console.WriteLine(ex.Message); } } }
Full Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SolPartialClass { public partial class PartialDemoClass { #region Method public void MethodA() { try { System.Console.WriteLine("Method A calling"); } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion } public partial class PartialDemoClass { #region Method public void MethodB() { try { System.Console.WriteLine("Method B Calling"); } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion } class Program { static void Main(string[] args) { try { PartialDemoClass Obj = new PartialDemoClass(); Obj.MethodA(); Obj.MethodB(); } catch (Exception ex) { System.Console.WriteLine(ex.Message); } } } }
Download
Download Source Code
No comments:
Post a Comment