Некоторое время назад я работал над проектом с Microsoft, когда у нас было посещение от кого-то в команде продукта CLR Microsoft.NET. Этот человек кодировал примеры и когда он определил свои переменные, он использовал “Int32” по сравнению с “int” и “String” по сравнению с “string”.
я не забыл видеть этот стиль в другом примере кода от Microsoft. Так, я провел некоторое исследование и нашел, что все говорят, что нет никакого различия между “Int32” и “int” за исключением окраски синтаксиса. На самом деле я нашел, что много материала, предлагающего Вас, использует “Int32” для создания кода более читаемым. Так, я принял стиль.
На днях я действительно находил различие! Компилятор doesn’t позволяет Вам вводить перечисление с помощью “Int32”, но это делает при использовании “int”. Don’t спрашивают меня почему, потому что я don’t знаю все же.
Пример:
public enum MyEnum : Int32
{
AEnum = 0
}
Это работает.
public enum MyEnum : int
{
AEnum = 0
}
Взятый от: нотация Int32 по сравнению с интервалом
Use the str2func
function (assumes the string in type
is the same as the name of the function):
p = str2func(type); % Create function handle using function name
c = p(phi, lambda); % Invoke function handle
NOTE: The documentation mentions these limitations:
Function handles created using
str2func
do not have access to variables outside of their local workspace or to nested functions. If your function handle contains these variables or functions, MATLAB® throws an error when you invoke the handle.
Use a SWITCH statement and function handles:
switch type
case 'Mercator'
p = @Mercator;
case 'KavrayskiyVII'
p = @KavrayskiyVII;
... % Add other cases as needed
end
c = p(phi, lambda); % Invoke function handle
Use EVAL and function handles (suggested by Andrew Janke):
p = eval(['@' type]); % Concatenate string name with '@' and evaluate
c = p(phi, lambda); % Invoke function handle
As Andrew points out, this avoids the limitations of str2func
and the extra maintenance associated with a switch statement.