Return Method Types

I am new to C # and I am trying to find a better way to check if the method will return correctly, i.e. this method got the information I wanted.

These are the types that make it difficult. Is there a way that I can sometimes return a bool and sometimes return something else?

In PHP, I would create my own function like this

function myFunc(){ //Do Stuff if(/*whatever*/) return (string) "abcdefg"; return false; //otherwise it will just return false } 

Then call it up to check if it works or not.

 if(!$var=myFunc()) die("It Didn't Work"); echo $var; 

But with C #, my function should return the specified type, not a string, or bool false if that didn't work.

If in C # I have a method that returns IntPtr, I could return it (IntPtr) 0 instead of false, but this is dirty and probably the best way to do this.

Any ideas? What is the standard and usual way to do this in C #?

+4
source share
6 answers

There are usually two approaches here:

The appropriate method depends on what it does. If a β€œfalse” case is indeed something exceptional, and not normal, then throwing an exception is the appropriate way to handle it.

If, however, you do something like parsing user input, where its common, which you "fail", using the TryXXX approach, is a good example. It will look like this:

 bool TryMyFunc(out string result) { if (...) { result = "abcdefg"; return true; } result = string.Empty; return false; } 

This is how Int32.TryParse works within a framework, for example.

+6
source

You can always return null.

 string Foo() { if (!success) return null; return "abcd"; } 

Then you test the call as follows:

 var tmp = Foo(); if (tmp == null) return -1; // tmp is a string so do your work 

Returning null will only work with the object, unfortunately, you cannot do this with int. In this case, throw an exception or use nullable int:

 int? Bar() 
+4
source

In your example, I am returning null from your function in C #:

 public string myFunc(){ //Do Stuff if(/*whatever*/) return "abcdefg"; return null; } 

And then in your code code:

 string myVar = myFunc(); if(null != myVar) { throw new Exception(); } Console.WriteLine(myVar); 

It should be noted that at any time you make exceptions for non-exceptional circumstances (which means that if your myFunc call could return null if used wisely), you will get a serious performance hit. It takes a long time to throw an exception, so it's best to avoid them, perhaps by using a preliminary check before calling myFunc.

In conditions when you are dealing with non-nullable value types, C # has the concept of Nullable Types .

+1
source

If your intention is to return types, you can use Tuple <>

 public Tuple<string,bool> myFunc() { //Do Stuff if(/*whatever*/) return (string) new Tuple<string,bool>("abcdefg",true); return new Tuple<string,bool>(null,false); } 
+1
source

Use exception-based programming:

 public string Blah() { if (whatever) { return "abcdefg"; } throw new Exception("Something went wrong"); } 

Or you can return null or string.Empty

0
source

Personally, I prefer to use null.

 public string foo() { if (whatever) { return "abc"; } return null; } 

and then:

  if(foo() != null) { //do something } else { // *It Didn't Work* } 
0
source

Source: https://habr.com/ru/post/1381777/


All Articles