[{"data":1,"prerenderedAt":664},["ShallowReactive",2],{"article-design-guidelines-classes-vs-structures":3},{"article":4,"tags":396,"previous":411,"next":502},{"id":5,"title":6,"author":7,"body":8,"createdAt":385,"description":386,"extension":387,"img":380,"meta":388,"navigation":389,"path":390,"seo":391,"stem":392,"tags":393,"updatedAt":385,"__hash__":395},"articles\u002Farticles\u002Fdesign-guidelines-classes-vs-structures.md","Design Guidelines– Classes vs. Structures","[object Object]",{"type":9,"value":10,"toc":382},"minimark",[11,27,140,155,160,162,191,312,318,347,350,372,376],[12,13,14,15,18,22,23,26],"p",{},"My goal is to cover some .NET 4 Platform features through a series of ‘guideline’ type entries demonstrating some new and existing functionality.  In this first entry, I have seen both classes and structures used throughout applications.  The following is meant to describe and explain good design considerations.",[16,17],"br",{},[19,20,21],"strong",{},"Structure"," types are well suited for modeling mathematical, geometrical, and other \"atomic\" entities in your application. A ",[19,24,25],{},"structure"," (like an enumeration) is a user-defined type; however, structures are not simply a collection of name\u002Fvalue pairs. Rather, structures are types that can contain any number of data fields and members that operate on these fields.",[28,29,32,33,32],"div",{"id":30,"style":31},"codeSnippetWrapper","text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;","  \n",[28,34,32,37,32,32,47,32,32,55,32,32,65,32,32,72,32,32,75,32,32,80,32,32,88,32,32,91,32,32,94,32,32,96,32,32,101,32,32,108,32,32,111,32,32,113,32,32,115,32,32,120,32,32,127,32,32,135,32,32,137,32],{"id":35,"style":36},"codeSnippet","text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;",[38,39,41,46],"pre",{"style":40},"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;",[42,43,45],"span",{"style":44},"color: #0000ff;","struct"," Point {",[38,48,50,51],{"style":49},"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;","    ",[42,52,54],{"style":53},"color: #008000;","\u002F\u002F Fields of the structure.",[38,56,50,57,60,61,64],{"style":40},[42,58,59],{"style":44},"public"," ",[42,62,63],{"style":44},"int"," X;",[38,66,50,67,60,69,71],{"style":49},[42,68,59],{"style":44},[42,70,63],{"style":44}," Y;",[38,73,74],{"style":40}," ",[38,76,50,77],{"style":49},[42,78,79],{"style":53},"\u002F\u002F Add 1 to the (X, Y) position.",[38,81,50,82,60,84,87],{"style":40},[42,83,59],{"style":44},[42,85,86],{"style":44},"void"," Increment() {",[38,89,90],{"style":49},"        X++; Y++;",[38,92,93],{"style":40},"    }",[38,95,74],{"style":49},[38,97,50,98],{"style":40},[42,99,100],{"style":53},"\u002F\u002F Subtract 1 from the (X, Y) position.",[38,102,50,103,60,105,107],{"style":49},[42,104,59],{"style":44},[42,106,86],{"style":44}," Decrement() {",[38,109,110],{"style":40},"        X--; Y--;",[38,112,93],{"style":49},[38,114,74],{"style":40},[38,116,50,117],{"style":49},[42,118,119],{"style":53},"\u002F\u002F Display the current position.",[38,121,50,122,60,124,126],{"style":40},[42,123,59],{"style":44},[42,125,86],{"style":44}," Display() {",[38,128,129,130,134],{"style":49},"        Console.WriteLine(",[42,131,133],{"style":132},"color: #006080;","\"X = {0}, Y = {1}\"",", X, Y);",[38,136,93],{"style":40},[38,138,139],{"style":49},"}",[12,141,142,143,145,146,148,149,151,152,154],{},"myPoint.X=100;\nmyPoint.Y=76;\nmyPoint.Display();",[16,144],{},"\nYou can create a structure using new keyword which will use the structures’ default constructor.",[16,147],{},"\ni.e. Point myPoint = new Point(); \u002F\u002F each field will be automatically set to its default value",[16,150],{},"\nA custom constructor can also be created with a structure which allows you to specify the values of a field upon creation.",[16,153],{},"\ni.e. Public Point(int xpos, int ypos)\nPoint p2 = new Point(50,50);",[12,156,157],{},[19,158,159],{},"Class",[19,161],{},[12,163,164,166,167,171,172,175,176,178,179,181,184,185,187,190],{},[19,165],{},"A class is a user-defined type that is composed of field data (often called ",[168,169,170],"em",{},"member variables",") and members that operate on this data (such as constructors, properties, methods, events, and so forth). Collectively, the set of field data represents the \"state\" of a class instance (otherwise known as an ",[168,173,174],{},"object","). The power of object-based languages such as C# is that by grouping data and related functionality in a unified class definition, you are able to model your software after entities in the real world.",[16,177],{},"\nI am going to jump to some less known aspects of a class.",[16,180],{},[168,182,183],{},"Default constructor",": By definition, a default constructor never takes arguments.  After allocating the new object into memory, the default constructor ensures that all field data of the class is set to an appropriate default value.\nCustom constructor:Allows the user with a simple and consistent way to initialize the state of an object directly at the time of creation.  Note however that as soon as you define a custom constructor the compiler removes the default constructor.  Thereby if you wish to continue to have both a default and custom constructors you must explicitly declare the default constructor.",[16,186],{},[168,188,189],{},"Chaining constructors ","utilizes the ‘this’ keyword.  Understand that using the this keyword to chain constructor calls is never mandatory.  Notice in the example below however that the constructor Public Motorcycle(string name) takes one string argument but continues to call the additional constructor Public Motorcyle(int intensity, string name) via the this keyword i.e. this(0,name)",[28,192,32,193,32],{"id":30,"style":31},[28,194,32,195,32,32,201,32,32,204,32,32,212,32,32,220,32,32,222,32,32,227,32,32,232,32,32,240,32,32,251,32,32,253,32,32,260,32,32,265,32,32,267,32,32,272,32,32,281,32,32,284,32,32,290,32,32,293,32,32,296,32,32,298,32,32,301,32,32,304,32,32,307,32,32,310,32],{"id":35,"style":36},[38,196,197,200],{"style":40},[42,198,199],{"style":44},"class"," Motorcycle",[38,202,203],{"style":49},"{",[38,205,206,207,60,209,211],{"style":40},"  ",[42,208,59],{"style":44},[42,210,63],{"style":44}," driverIntensity;",[38,213,206,214,60,216,219],{"style":49},[42,215,59],{"style":44},[42,217,218],{"style":44},"string"," driverName;",[38,221,74],{"style":40},[38,223,206,224],{"style":49},[42,225,226],{"style":53},"\u002F\u002F Constructor chaining.",[38,228,206,229,231],{"style":40},[42,230,59],{"style":44}," Motorcycle() {}",[38,233,206,234,236,237,239],{"style":49},[42,235,59],{"style":44}," Motorcycle(",[42,238,63],{"style":44}," intensity)",[38,241,242,243,246,247,250],{"style":40},"    : ",[42,244,245],{"style":44},"this","(intensity, ",[42,248,249],{"style":132},"\"\"",") {}",[38,252,74],{"style":49},[38,254,206,255,236,257,259],{"style":40},[42,256,59],{"style":44},[42,258,218],{"style":44}," name)",[38,261,242,262,264],{"style":49},[42,263,245],{"style":44},"(0, name) {}",[38,266,74],{"style":40},[38,268,206,269],{"style":49},[42,270,271],{"style":53},"\u002F\u002F This is the 'master' constructor that does all the real work.",[38,273,206,274,236,276,278,279,259],{"style":40},[42,275,59],{"style":44},[42,277,63],{"style":44}," intensity, ",[42,280,218],{"style":44},[38,282,283],{"style":49},"  {",[38,285,50,286,289],{"style":40},[42,287,288],{"style":44},"if"," (intensity > 10)",[38,291,292],{"style":49},"    {",[38,294,295],{"style":40},"      intensity = 10;",[38,297,93],{"style":49},[38,299,300],{"style":40},"    driverIntensity = intensity;",[38,302,303],{"style":49},"    driverName = name;",[38,305,306],{"style":40},"  }",[38,308,309],{"style":49},"...",[38,311,139],{"style":40},[12,313,314,317],{},[168,315,316],{},"Optional Arguments"," allow you to define supplied default values to the incoming arguments.  i.e. with the constructor below the user can create a new RocketShip with zero, one or two arguments.  This is a .NET 4 feature only.\nRocketShip = new RocketShip();\nRocketShip = new RocketShip(name:=”TestShip”);\nRocketShip = new RocketShip(10); \u002F\u002Fsets speed = 10, leaving name =  ””",[28,319,32,320,32],{"id":30,"style":31},[28,321,32,322,32,32,327,32,32,341,32,32,344,32,139],{"id":35,"style":36},[38,323,324],{"style":40},[42,325,326],{"style":53},"\u002F\u002F Single constructor using optional args.",[38,328,329,331,332,334,335,337,338,340],{"style":49},[42,330,59],{"style":44}," RocketShip(",[42,333,63],{"style":44}," speed = 0, ",[42,336,218],{"style":44}," name = ",[42,339,249],{"style":132},"){",[38,342,343],{"style":49},"  driverSpeed = speed;",[38,345,346],{"style":40},"  driverName = name;",[19,348,349],{},"Classes vs. Structures Guidelines",[351,352,353,357,360,363,366,369],"ul",{},[354,355,356],"li",{},"Consider defining a structure instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.",[354,358,359],{},"Do not define a structure unless the type has all the following characteristics:",[354,361,362],{},"It logically represents a single value, similar to primitive types (integer, double, and so on).",[354,364,365],{},"It has an instance size smaller than 16 bytes.",[354,367,368],{},"It is immutable.",[354,370,371],{},"It will not have to be boxed frequently.",[373,374,375],"blockquote",{},"  \nIf one or more of these conditions are not met, create a reference type instead of a structure. Failure to adhere to this guideline can negatively impact performance.  \n",[377,378],"img",{"style":379,"src":380,"alt":381},"display: none;","\u002Farticles\u002Fimages\u002Fstructure2.jpg","",{"title":381,"searchDepth":383,"depth":383,"links":384},2,[],"2015-04-20T08:07:14.6400000-04:00","My goal is to cover some .NET 4 Platform features through a series of &lsquo;guideline&rsquo; type entries demonstrating some new and existing functionality.","md",{},true,"\u002Farticles\u002Fdesign-guidelines-classes-vs-structures",{"title":6,"description":386},"articles\u002Fdesign-guidelines-classes-vs-structures",[394],"netcore","-N3gPMr8ZsjnQA0jdKCMNLjbID8lUwbxN-6kUVEFlhA",[397],{"id":398,"title":399,"body":400,"description":404,"extension":387,"img":405,"meta":406,"name":394,"navigation":389,"path":407,"seo":408,"stem":409,"__hash__":410},"tags\u002Ftags\u002Fnetcore.md","Netcore",{"type":9,"value":401,"toc":402},[],{"title":381,"searchDepth":383,"depth":383,"links":403},[],".NET Core is a new version of .NET Framework, which is a free, open-source, general-purpose development platform maintained by Microsoft. It is a cross-platform framework that runs on Windows, macOS, and Linux operating systems.","https:\u002F\u002Fimages.unsplash.com\u002Fphoto-1598313183973-4effcded8d5e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=675&q=80",{},"\u002Ftags\u002Fnetcore",{"description":404},"tags\u002Fnetcore","D5BWCPpKVXJTUKU0TRuD3sWQ9rXtqETGkxzHAK__g5w",{"id":412,"title":413,"author":7,"body":414,"createdAt":493,"description":494,"extension":387,"img":489,"meta":495,"navigation":389,"path":496,"seo":497,"stem":498,"tags":499,"updatedAt":493,"__hash__":501},"articles\u002Farticles\u002Fcanadian-developers-where-are-you.md","Canadian Developers–Where are you?",{"type":9,"value":415,"toc":491},[416,434,487],[12,417,418,419,425,426,429,433],{},"Okay, after an online search for the Canadian developer ‘pulse’ I resorted to twitter to aid in my search.  My calls were heard by  ",[420,421,424],"a",{"href":422,"target":423},"http:\u002F\u002Ftwitter.com\u002Fjbristowe","_blank","@jbristowe"," and in return he provided a great twitter and blog list of developers in Canada that he was aware of (",[420,427],{"href":428},"http:\u002F\u002Fblogs.msdn.com\u002Fb\u002Fcdndevs\u002Farchive\u002F2010\u002F10\u002F03\u002Fdevelopers-in-canada-you-should-follow-on-twitter.aspx",[420,430,428],{"href":428,"rel":431},[432],"nofollow",")",[12,435,436,437,60,440,443,446,447,450,451,454,457,459,460,462,463,465,466,462,468,470,471,473,474,477,481,482,484],{},"In return, I created a ",[19,438,439],{},"twitter list",[420,441],{"href":442},"http:\u002F\u002Ftwitter.com\u002F#\u002Flist\u002Fdyardy\u002Fcdndevs",[420,444,442],{"href":442,"rel":445},[432]," so others can connect and communicate much easier.  In addition I created an RSS feed for all the respective blogs\u002Frss feeds associated with the developers on the twitter list.  The ",[19,448,449],{},"Google Reader (RSS) bundle"," link can be found here ",[420,452],{"href":453},"http:\u002F\u002Fwww.google.com\u002Freader\u002Fbundle\u002Fuser\u002F04940798407978267246\u002Fbundle\u002FCanadian%20Developers%20via%20%40dyardy",[420,455,453],{"href":453,"rel":456},[432],[16,458],{},"\nI will be maintaining both of these lists, so please let me know if you are missing from the list (or would like to be added).",[16,461],{},"\n ",[16,464],{},"\nI grew up around Peterborough, Ontario and graduated from Queen’s Engineering.  I often return to Ontario to visit with friends and family.  I would love to develop a relationship with developers in Canada and connect with them at user groups or developer events.",[16,467],{},[16,469],{},"\nReferences:",[16,472],{},"\nLinkedIn Discussion Group (for Canadian Developers) ",[420,475],{"href":476},"http:\u002F\u002Fwww.linkedin.com\u002Fgroups?mostPopular=&gid=3398140",[420,478,479],{"href":479,"rel":480},"http:\u002F\u002Fwww.linkedin.com\u002Fgroups?mostPopular=&amp;gid=3398140",[432],"\nMicrosoft Canadian Evangelist list ",[420,483],{"href":428},[420,485,428],{"href":428,"rel":486},[432],[377,488],{"src":489,"alt":381,"style":490},"\u002Farticles\u002Fimages\u002Fcanada+flag.jpg","display:none;",{"title":381,"searchDepth":383,"depth":383,"links":492},[],"2015-04-20T08:07:14.7500000-04:00",null,{},"\u002Farticles\u002Fcanadian-developers-where-are-you",{"title":413,"description":494},"articles\u002Fcanadian-developers-where-are-you",[500],"general","S3ujHYMDowqAGkxJLoSbESKd46cl_pAJzSQJiPewK0U",{"id":503,"title":504,"author":7,"body":505,"createdAt":655,"description":656,"extension":387,"img":494,"meta":657,"navigation":389,"path":658,"seo":659,"stem":660,"tags":661,"updatedAt":655,"__hash__":663},"articles\u002Farticles\u002Ffeature-eventreceiver-get-web-context.md","Feature.EventReceiver Get Web Context",{"type":9,"value":506,"toc":653},[507,548],[12,508,509,510,512,513,515,516,518,519,521,522,462,524,526,531,532,535,536,539,540,543,544,547],{},"In order to get the current SPWeb context that the feature was activated on I have used the following extension method.  Regardless if the feature is web or site scoped the following GetWeb extension will return the current web context.",[16,511],{},"\nTypically, you would do something like the following.  This works fine if the feature is scoped for web.  If however it is scoped for Site then it will no longer work (.Parent becomes SPSite)",[16,514],{},"\nSPWeb site = (SPWeb)properties.Feature.Parent; ",[16,517],{},"\nUsing the following extension you can now use something like the following.",[16,520],{},"\nSPWeb web = properties.GetWeb();  \u002F\u002Fmuch cleaner",[16,523],{},[16,525],{},[42,527,530],{"className":528},[529],"kwrd","using"," System;\n",[42,533,530],{"className":534},[529]," System.Collections.Generic;\n",[42,537,530],{"className":538},[529]," System.Linq;\n",[42,541,530],{"className":542},[529]," System.Text;\n",[42,545,530],{"className":546},[529]," Microsoft.SharePoint;",[12,549,550,60,553,60,557,560,561,566,570,574,578,582,60,585,588,589,597,598,601,602,605,606,610,611,60,615,605,618,621,622,625,626,60,630,634,635,640,641,645,646,648,649,462,651,462],{},[42,551,59],{"className":552},[529],[42,554,556],{"className":555},[529],"static",[42,558,199],{"className":559},[529]," Extensions\n{\n",[42,562,565],{"className":563},[564],"rem","\u002F\u002F\u002F \u003Csummary>",[42,567,569],{"className":568},[564],"\u002F\u002F\u002F Gets the web.",[42,571,573],{"className":572},[564],"\u002F\u002F\u002F \u003C\u002Fsummary>",[42,575,577],{"className":576},[564],"\u002F\u002F\u002F \u003Cparam name=\"properties\">The properties.\u003C\u002Fparam>",[42,579,581],{"className":580},[564],"\u002F\u002F\u002F \u003Creturns>\u003C\u002Freturns>",[42,583,59],{"className":584},[529],[42,586,556],{"className":587},[529]," SPWeb\n",[42,590,592,593],{"className":591},[564],"\u002F\u002F",[420,594,595],{"href":595,"rel":596},"http:\u002F\u002Fblog.mattsmith.co.nz\u002FLists\u002FPosts\u002FPost.aspx?List=c7bdac80-1d4e-4732-9e67-cefde9c03d31&amp;ID=51",[432],"\nGetWeb(",[42,599,245],{"className":600},[529]," SPFeatureReceiverProperties properties){\n    SPWeb site;\n    ",[42,603,288],{"className":604},[529]," (properties.Feature.Parent ",[42,607,609],{"className":608},[529],"is"," SPWeb) {\n        site = (SPWeb)properties.Feature.Parent;\n    } ",[42,612,614],{"className":613},[529],"else",[42,616,288],{"className":617},[529],[42,619,609],{"className":620},[529]," SPSite) {\n        site = ((SPSite)properties.Feature.Parent).RootWeb;\n    } ",[42,623,614],{"className":624},[529]," {\n        ",[42,627,629],{"className":628},[529],"throw",[42,631,633],{"className":632},[529],"new"," Exception(",[42,636,639],{"className":637},[638],"str","\"Unable to retrieve SPWeb - this feature is not Site or Web-scoped.\"",");\n    }\n    ",[42,642,644],{"className":643},[529],"return"," site;\n    }\n}",[16,647],{},"\nCool eh?",[16,650],{},[16,652],{},{"title":381,"searchDepth":383,"depth":383,"links":654},[],"2015-04-20T08:07:14.5400000-04:00","In order to get the current SPWeb context that the feature was activated...",{},"\u002Farticles\u002Ffeature-eventreceiver-get-web-context",{"title":504,"description":656},"articles\u002Ffeature-eventreceiver-get-web-context",[662],"sharepoint","0dvtWcX_WjOm6yWA2aUZbBzFdRBtbbC3K1kZCZu1pz4",1781574765795]