Category Archives: C# Programming

Chapter90:ExpressionTrees

Parameter                                     Details TDelegate                    Thedelegatetypetobeusedfortheexpression lambdaExpression Thelambdaexpression(ex.num=>num<5) ExpressionTreesareExpressionsarrangedinatreelikedatastructure.Eachnodeinthetreeisarepresentationof an expression, an expression being code. An In-Memory representation of a Lambda expression would be an…
Continue reading

Chapter89:Events

Parameter                                                       Details EventArgsT             ThetypethatderivesfromEventArgsandcontainstheeventparameters. EventName             The name of the event. HandlerName          The name of the event handler. SenderObject         Theobjectthat'sinvokingtheevent. EventArgumentsAninstanceoftheEventArgsTtypethatcontainstheeventparameters. Aneventisanotificationthatsomethinghasoccurred(suchasamouseclick)or,insomecases,isabouttooccur (such as…
Continue reading

Chapter88:INotifyPropertyChanged interface

Section88.1:ImplementingINotifyPropertyChangedinC#6 TheimplementationofINotifyPropertyChangecanbeerror-prone,astheinterfacerequiresspecifyingproperty nameasastring.Inordertomaketheimplementationmorerobust,anattributeCallerMemberNamecanbeused. classC:INotifyPropertyChanged { //backingfield intoffset; //property publicintOffset { get { returnoffset; } set { if(offset==value) return; offset=value; RaisePropertyChanged(); } } //helpermethodforraisingPropertyChangedevent…
Continue reading

Chapter87:InitializingProperties

Section87.1:C#6.0:InitializeanAuto-ImplementedProperty Createapropertywithgetterand/orsetterandinitializeallinoneline: publicstringFoobar{get;set;}="xyz"; Section87.2:InitializingPropertywithaBackingField publicstringFoobar{ get{return_foobar;} set{_foobar=value;} } privatestring_foobar="xyz"; Section87.3:PropertyInitializationduringobjectinstantiation Propertiescanbesetwhenanobjectisinstantiated. varredCar=newCar { Wheels=2, Year=2016,Color=C olor.Red }; Section87.4:InitializingPropertyinConstructor classExample { publicstringFoobar{get;set;} publicList<string>Names{get;set;} publicExample()…
Continue reading

Chapter86:Properties

Section86.1:Auto-implementedproperties Auto-implementedpropertieswereintroducedinC#3. Anauto-implementedpropertyisdeclaredwithanemptygetterandsetter(accessors): publicboolIsValid{get;set;} When an auto-implemented property is written in your code, the compiler creates a private anonymous field that can only…
Continue reading

Chapter85:GenericLambdaQueryBuilder

Section85.1:QueryFilterclass Thisclassholdspredicatefiltersvalues. publicclassQueryFilter { publicstringPropertyName{get;set;} publicstringValue{get;set;} publicOperatorOperator{get;set;} //Inthequery{a=>a.Name.Equals("Pedro")} //Propertynametofilter-propertyName="Name" //Filtervalue-value="Pedro" //Operationtoperform-operation=enumOperator.Equals publicQueryFilter(stringpropertyName,stringvalue,OperatoroperatorValue) { PropertyName=propertyName; Value=value; Operator=operatorValue; } } Enumtoholdtheoperationsvalues: publicenumOperator { Contains, GreaterThan,…
Continue reading

Chapter84:Lambdaexpressions

Section84.1:LambdaExpressionsasShorthandforDelegate Initialization publicdelegateintModifyInt(intinput); ModifyIntmultiplyByTwo=x=>x*2; TheaboveLambdaexpressionsyntaxisequivalenttothefollowingverbosecode: publicdelegateintModifyInt(intinput); ModifyIntmultiplyByTwo=delegate(intx){ returnx*2; }; Section84.2:LambdaExpressionasanEventHandler Lambdaexpressionscanbeusedtohandleevents,whichisusefulwhen: The handler is short. The handler never needs to be unsubscribed. Agoodsituationinwhichalambdaeventhandlermightbeusedisgivenbelow:…
Continue reading

Chapter 83:Using json.net

UsingJSON.netJsonConverterclass. Section83.1:UsingJsonConverteronsimplevalues Example using JsonCoverter to deserialize the runtime property from the api response into a TimespanObject in the Movies model JSON(http://www.omdbapi.com/?i=tt1663662) {…
Continue reading

Chapter82:GettingStarted:JsonwithC#

ThefollowingtopicwillintroduceawaytoworkwithJsonusingC#languageandconceptsofSerializationand Deserialization. Section82.1:SimpleJsonExample { "id":89, "name":"AldousHuxley", "type":"Author", "books":[{ "name":"BraveNewWorld", "date":1932 }, { "name":"EyelessinGaza", "date":1936 }, { "name":"TheGeniusandtheGoddess", "date":1955 }] } IfyouarenewintoJson,hereisanexemplifiedtutorial. Section82.2:FirstthingsFirst:LibrarytoworkwithJson ToworkwithJsonusingC#,itisneedtouseNewtonsoft(.netlibrary).Thislibraryprovidesmethodsthatallowsthe…
Continue reading

Chapter81:Overflow

Section81.1:Integeroverflow Thereisamaximumcapacityanintegercanstore.Andwhenyougooverthatlimit,itwillloopbacktothenegative side. For int, it is 2147483647 intx=int.MaxValue; //MaxValueis2147483647 x=unchecked(x+1); //makeoperationexplicitlyuncheckedsothattheexamplealso workswhenthecheckforarithmeticoverflow/underflowisenabledintheprojectsettings Console.WriteLine(x);    //Willprint-2147483648 Console.WriteLine(int.MinValue);             //SameasMinvalue ForanyintegersoutofthisrangeusenamespaceSystem.NumericswhichhasdatatypeBigInteger.Checkbelow linkformoreinformationhttps://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx Section81.2:Overflowduringoperation Overflowalsohappensduringtheoperation.Inthefollowingexample,xisanint,1isanintbydefault.Therefore addition is…
Continue reading