TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# partial Keyword

Use the partial class modifier. Partial classes are spread out among several files.
Partial. Partial classes span multiple files. With the partial keyword in the C# language, you can physically separate a class into multiple files.ClassKeywords
Notes, partial. This keyword is often used by code generators (like Visual Studio). But we can sometimes benefit from directly using partial on a C# class declaration.
An example. Normally you cannot declare a class in 2 separate files. But with the partial modifier, you can. This is useful if one file is commonly edited and the other is not.

Here: If you remove the partial modifier, you will get an error at compile time.

Error text: The namespace "<global namespace>" already contains a definition for "A".

Namespace

Tip: To fix this, you can either use the partial keyword, or change one of the class names.

C# program that uses partial class class Program { static void Main() { A.A1(); A.A2(); } } Contents of file A1.cs: C# using System; partial class A { public static void A1() { Console.WriteLine("A1"); } } Contents of file A2.cs: C# using System; partial class A { public static void A2() { Console.WriteLine("A2"); } } Output A1 A2
Compiler. How does the C# compiler deal with partial classes? If you disassemble the above program, you will see that the files A1.cs and A2.cs are eliminated.

And: You will find that the class A is present. Class A will contain the methods A1 and A2 in the same code block.

Thus: Partial classes are precisely equivalent to a single class with all the members.

Compiled result of A1.cs and A2.cs: C# internal class A { // Methods public static void A1() { Console.WriteLine("A1"); } public static void A2() { Console.WriteLine("A2"); } }
A review. Partial classes can simplify certain situations. They are often used in Visual Studio when creating Windows Forms programs. The machine-generated C# code is separate.
Some uses. Partial classes separate commonly-edited code from rarely-edited code. This can reduce confusion and the possibility that code that isn't supposed to be edited is changed.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf