Field initializers & conditional Operator

Source: Internet
Author: User

Field initializers

You can assign values directly to values or objects.

We recommend that you do not assign values to the filed initialzer directly. When Inheritance occurs, the order will be weird:

Solution: assign values to filed in Constructor

Note that if it is an inherited class, you must first use the ctor base class. When it is called public person (), it will jump to the above base class public base () first.

 Using  System;  Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. text;  Using System. Threading. tasks;  Namespace  Leleapplication2 {  Static   Class  Whoa {  Public   Static   Int Getint ( String   From  ) {Console. writeline (  "  Getting an int from {0}  " ,From  );  Return   4  ;}}  Class  Base {  Private   Int  _ Basestuff;  Public  Base () {_ basestuff = Whoa. getint ( "  Base. _ basestuff  " ); Whoa. getint (  "  From base ctor  "  );}}  Class  Person: Base {  Private   Int  _ Health;  Private   Int  _ Mana;  Private   Int  _ Whoa; Public  Person () {_ health = Whoa. getint ( "  Person. _ health  "  ); _ Mana = Whoa. getint ( "  Person. _ mana  "  ); _ Whoa = Whoa. getint ( "  Person. _ Whoa  "  ); Whoa. getint ( "  From person ctor  "  );}}  Class  Program {  Static   Void Main ( String  [] ARGs ){  VaR P = New  Person (); console. readkey ();}}} 

View code

Conditional Operator

 

Level-1 operator:!

Secondary operators:> =

Third-level operators:Condition expression1: expression2 any type can be put in expression except void. The types of the two expressions must be convertible to each other.

 VaR Whoa = age> = 21 ? Int . Parse ( "  123  " ):123 ; //  The returned value is int.  VaR Db1 = age> = 21 ? Stuff (): Whoa (); //  The returned value is double.  Static   Int  Stuff (){  Return   4 ;} Static   Int  Whoa (){ Return   32.3232  ;}

VaRToprint = age> =21? 34141.Tostring():"You are not buy this";

Console.Writeline("You {0} buy alcohol in the States!", Age> = 21 :"Can","Can't");// The best way to write

Note the limitations on operands: mutual conversion is required. For example, if 's' and 23.2 are converted to the double type, the return value of this three-level operation is a double.

The two operands of alchohol are strings. Therefore, the three-level operation returns a string type, which can be found in console. writeline.

The following is a complex example:

 

Null coalescing Operator

 

Return account ?? Notloggedinaccount; equivalent

Return account! = NULL? Account: notloggedinaccount;

Whether the ask account is not null. If it is not returned on the left, if it is returned on the right

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.