null

Method call if not null in C#

感情迁移 提交于 2019-12-17 21:42:00
问题 Is it possible to somehow shorten this statement? if (obj != null) obj.SomeMethod(); because I happen to write this a lot and it gets pretty annoying. The only thing I can think of is to implement Null Object pattern, but that's not what I can do every time and it's certainly not a solution to shorten syntax. And similar problem with events, where public event Func<string> MyEvent; and then invoke if (MyEvent != null) MyEvent.Invoke(); 回答1: From C# 6 onwards, you can just use: MyEvent?.Invoke

Why does C# require you to write a null check every time you fire an event?

随声附和 提交于 2019-12-17 19:37:19
问题 This seems odd to me -- VB.NET handles the null check implicitly via its RaiseEvent keyword. It seems to raise the amount of boilerplate around events considerably and I don't see what benefit it provides. I'm sure the language designers had a good reason to do this.. but I'm curious if anyone knows why. 回答1: It's certainly a point of annoyance. When you write code which accesses a field-like event within a class, you're actually accessing the field itself (modulo a few changes in C# 4; let's

nil slice when passed as interface is not nil! Why? (golang)

末鹿安然 提交于 2019-12-17 19:01:40
问题 See this playground: http://play.golang.org/p/nWHmlw1W01 package main import "fmt" func main() { var i []int = nil yes(i) // output: true no(i) // output: false } func yes(thing []int) { fmt.Println(thing == nil) } func no(thing interface{}) { fmt.Println(thing == nil) } Why the difference in output between the two functions? 回答1: Admittedly, it's somewhat of a quirk, but there's an explanation for it. Imagine an interface{} variable as a struct composed of two fields: one is the type and

Why is an empty PowerShell pipeline not the same as null?

为君一笑 提交于 2019-12-17 18:58:38
问题 I am trying to understand the behavior of the @() array constructor, and I came across this very strange test. It seems that the value of an empty pipeline is "not quite" the same as $null, even though it is -eq $null The output of each statement is shown after the ### $y = 1,2,3,4 | ? { $_ -ge 5 } $z = $null if ($y -eq $null) {'y is null'} else {'y NOT null'} ### y is null if ($z -eq $null) {'z is null'} else {'z NOT null'} ### z is null $ay = @($y) $az = @($z) "ay.length = " + $ay.length ##

Is there any problem using self.property = nil in dealloc?

ε祈祈猫儿з 提交于 2019-12-17 18:56:21
问题 I know declared property generates accessor method which is someway just syntax sugar. I found quite a lot people use self.property = nil in their dealloc method. 1) In Apple's Memory Management document, p23 It says: The only places you shouldn’t use accessor methods to set an instance variable are in init methods and dealloc. why shouldn't? 2) In apple's Objective-C 2.0 , p74 Declared properties fundamentally take the place of accessor method declarations; when you synthesize a property,

How can I test an NSString for being nil?

試著忘記壹切 提交于 2019-12-17 18:31:55
问题 Can I simply use if(myString == nil) For some reason a string that I know is null, is failing this statement. 回答1: Is it possible that your string is not in fact nil , and is instead just an empty string? You could try testing whether [myString length] == 0 . 回答2: You can find more on objective C string here. + (BOOL ) stringIsEmpty:(NSString *) aString { if ((NSNull *) aString == [NSNull null]) { return YES; } if (aString == nil) { return YES; } else if ([aString length] == 0) { return YES;

Should I set the initial java String values from null to “”?

▼魔方 西西 提交于 2019-12-17 17:59:52
问题 Often I have a class as such: public class Foo { private String field1; private String field2; // etc etc etc } This makes the initial values of field1 and field2 equal to null. Would it be better to have all my String class fields as follows? public class Foo { private String field1 = ""; private String field2 = ""; // etc etc etc } Then, if I'm consistent with class definition I'd avoid a lot of null pointer problems. What are the problems with this approach? 回答1: I disagree with the other

Should the hash code of null always be zero, in .NET

可紊 提交于 2019-12-17 17:53:04
问题 Given that collections like System.Collections.Generic.HashSet<> accept null as a set member, one can ask what the hash code of null should be. It looks like the framework uses 0 : // nullable struct type int? i = null; i.GetHashCode(); // gives 0 EqualityComparer<int?>.Default.GetHashCode(i); // gives 0 // class type CultureInfo c = null; EqualityComparer<CultureInfo>.Default.GetHashCode(c); // gives 0 This can be (a little) problematic with nullable enums. If we define enum Season { Spring,

Release, Dealloc, and the Self reference

偶尔善良 提交于 2019-12-17 17:26:45
问题 So I thought I had all these questions all figured out. Then all of a sudden I get an error (a crash) I can't figure out. Then after doing research to remedy the crash, I notice everything that I thought I knew about these critical areas are somewhat wrong. Below are 8 questions I am just going to shoot out there in hopes of somebody answering - the answers to these will greatly help me get my understanding back on track. Thanks ahead of time! Q1) Is it okay to call Release on an object if

IBOutlet of another view controller is nil

南楼画角 提交于 2019-12-17 16:50:21
问题 I have a storyviewcontroller which has objects on its view. I need to change the text on the UILabel(In the storyviewcontroller) and the load the view on an array. I have connected the IBOutlet to the label in the storyviewcontroller. class StoryViewController: UIViewController { @IBOutlet weak var textLabel: UILabel! @IBOutlet weak var inspiredButton: UIButton! I have created an object of the storyviewcontroller class and am able to access its variables. However, after creating the object of