{"id":233,"date":"2014-08-26T11:36:03","date_gmt":"2014-08-26T11:36:03","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=233"},"modified":"2014-08-26T11:36:03","modified_gmt":"2014-08-26T11:36:03","slug":"partial-classpartial-method-and-anonymous-type","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=233","title":{"rendered":"Partial class,partial method and Anonymous Type"},"content":{"rendered":"<h1>Partial class&#8211;<\/h1>\n<p>It is possible to split the definition of a <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/0b0thckt.aspx\">class<\/a> or a <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/ah19swz4.aspx\">struct<\/a>, an <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/87d83y5b.aspx\">interface<\/a> or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.<\/p>\n<p>There are several situations when splitting a class definition is desirable:<\/p>\n<ul>\n<li>When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.<\/li>\n<li>When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio<\/li>\n<\/ul>\n<p>If any part is declared abstract, then the whole type is considered abstract. If any part is declared sealed, then the whole type is considered sealed. If any part declares a base type, then the whole type inherits that class. The partial modifier is not available on delegate or enumeration declarations.<\/p>\n<ul>\n<li>All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Partial definitions cannot span multiple modules.<\/li>\n<li>The class name and generic-type parameters must match on all partial-type definitions. Generic types can be partial. Each partial declaration must use the same parameter names in the same order.<\/li>\n<\/ul>\n<h1>Partial Method<\/h1>\n<p>A partial class or struct may contain a partial method. A partial method declaration consists of two parts: the definition, and the implementation<\/p>\n<p>\/\/ Definition in file1.cs<\/p>\n<p>partial void onNameChanged();<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Implementation in file2.cs<\/p>\n<p>partial void onNameChanged()<\/p>\n<p>{<\/p>\n<p>\/\/ method body<\/p>\n<p>}<\/p>\n<ul>\n<li>Partial method declarations must begin with the contextual keyword <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/wbx7zzdd.aspx\">partial<\/a> and the method must return <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/yah0tteb.aspx\">void<\/a>.<\/li>\n<li>Partial methods can have <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/14akc2c7.aspx\">ref<\/a> but not <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/t3c3bfhx.aspx\">out<\/a> parameters.<\/li>\n<li>Partial methods are implicitly <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/st6sy9xe.aspx\">private<\/a>, and therefore they cannot be <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/9fkccyh4.aspx\">virtual<\/a>.<\/li>\n<li>Partial methods cannot be <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/e59b22c5.aspx\">extern<\/a>, because the presence of the body determines whether they are defining or implementing.<\/li>\n<li>Partial methods can have <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/98f28cdx.aspx\">static<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/chfa2zb8.aspx\">unsafe<\/a> modifiers.<\/li>\n<\/ul>\n<h1>Anonymous Types<\/h1>\n<p>Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.<\/p>\n<p>we create anonymous types by using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/51y09td4.aspx\">new<\/a> operator together with an object initializer.<\/p>\n<p>var v = new { Amount = 108, Message = &#8220;Hello&#8221; };<\/p>\n<p>\/\/v.Amount = 13;&#8211;cannot change due to readonly only property<\/p>\n<p>\/\/ Rest the mouse pointer over v.Amount and v.Message in the following<\/p>\n<p>\/\/ statement to verify that their inferred types are int and string.<\/p>\n<p>Console.WriteLine(v.Amount + v.Message);<\/p>\n<p>Anonymous types typically are used in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb384087.aspx\">select<\/a> clause of a query expression to return a subset of the properties from each object in the source sequence.<\/p>\n<p>&#8211;following example use in linq.<\/p>\n<p>var productQuery =<\/p>\n<p>from prod in products select\u00a0new { prod.Color, prod.Price };<\/p>\n<p>we cannot declare a field, a property, an event, or the return type of a method as having an anonymous type. Similarly, we cannot declare a formal parameter of a method, property, constructor, or indexer as having an anonymous type. To pass an anonymous type, or a collection that contains anonymous types, as an argument to a method, we can declare the parameter as type object. However, doing this defeats the purpose of strong typing. If we must store query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.<\/p>\n<p>Because the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.object.equals.aspx\">Equals<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.object.gethashcode.aspx\">GetHashCode<\/a> methods on anonymous types are defined in terms of the Equals and GetHashCode methods of the properties, two instances of the same anonymous type are equal only if all their properties are equal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Partial class&#8211; It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled. There are several situations when splitting a class definition is desirable: When&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=233\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,11],"tags":[],"class_list":["post-233","post","type-post","status-publish","format-standard","hentry","category-c-vb","category-oop","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=233"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}