有趣的代码片段

时间:2016-12-10 22:55:37

标签: c# function methods compiler-errors return

我一直认为一个不同于void的方法必须返回一个值,无论如何,如果我们有这个例如

SettingsActivity.java

Intent intent;
static PendingIntent recurringDownload;

intent  = new Intent(context, UpdateScoresService.class);

recurringDownload = PendingIntent.getService(context, 0, intent, 0);
 Preference.OnPreferenceChangeListener refreshListener = new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if(newValue.toString().equals("1")){ /* daily */
                        background_refresh.setSummary("Scores will be refreshed daily.");
                        AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                        manager.cancel(recurringDownload);
                        recurringDownload.cancel();
                        Log.e("DAILY REFRESH", " ");
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(System.currentTimeMillis());
                        calendar.set(Calendar.HOUR_OF_DAY,10);
                        calendar.set(Calendar.MINUTE,00);
                        if(calendar.before(Calendar.getInstance())){
                            Log.e("AFTER", "10 AM DAILY");
                            calendar.add(Calendar.DATE, 1);
                        }
                        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringDownload);
                    }else if(newValue.toString().equals("2")){ /* weekly */
                        Log.e("WEEKLY REFRESH", " ");
                        background_refresh.setSummary("Scores will be refreshed weekly.");
                        AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                        manager.cancel(recurringDownload);
                        recurringDownload.cancel();
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(System.currentTimeMillis());
                        calendar.set(Calendar.HOUR_OF_DAY,10);
                        calendar.set(Calendar.MINUTE,00);
                        if(calendar.before(Calendar.getInstance())){
                            Log.e("AFTER", "10 AM WEEKLY");
                            calendar.add(Calendar.DATE, 1);
                        }
                        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, recurringDownload);
                    }else{ /* manually */
                        background_refresh.setSummary("Scores will be refreshed manually.");
                        Log.e("MANUAL REFRESH", " ");
                        AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                        manager.cancel(recurringDownload);
                        recurringDownload.cancel();

                    }
                    return true;
                }
};

这里没有编译时错误,我认为这是因为循环是无限的C#可以保证它不会在没有返回值的情况下终止。我是对的吗?

2 个答案:

答案 0 :(得分:2)

是的,如果该函数将永远运行,则返回值是多余的,因此编译器只是忽略它。

答案 1 :(得分:0)

只有具有reachable端点的非空函数必须返回一个值。

有关详细信息,请查看Eric Lippert的this answer