class HogeClass
# class variable = array type
@@class_variable=[]
# Instance variable = associative array type
@instance_variable={}
# constructor
def initialize (name)
@name = name
end
# instance method
def instance_method_name(argument)
# Calling Class Methods
class_method_name();
# Add value to class variable
@@class_variable<<arg
# Output by referring to class variables
p@@class_variable
# Output referencing instance variables
p@instance_variable
end
# class method
def self.class_method_name
end
private
# private method
default_method_name
end
end
I tried my best to write
I don't know class variables, instance garden numbers, etc.
Other than that, I'm not myself.
public class HogeClass{
public void instance_method(argument){
}
private void private_method_name(){
}
}
void
is the same as ruby's def
...
Please let me know if you have any questions such as writing with C# or using C#-like writing.
Thank you for your cooperation.
ruby c#
First, describe the namespace to use at the beginning of the file.
// For Console
using System;
// For ArrayList and Hashtable
using System.Collections;
class HogeClass
{
Declare the field with the access qualifier + type name + field name and set the initial value, if necessary.For class variables, the qualifier is static
.
Since the C# array is fixed in length, use ArrayList
in the variable-length list that accepts any type for porting.If the type can be limited, use the generic List<T>
type.
//@@class_variable=[ ]
static ArrayList class_variable = new ArrayList();
Use Hashtable
for non-generic associative arrays and Dictionary<Tkey,TValue>
for generic types.
//@instance_variable={}
Hashtable instance_variable=new Hashtable();
The constructor declares with the access qualifier + type name + argument.Also explicitly specify the field name
and the type of argument name
(string
?).
//# constructor
// def initialize (name)
// @name = name
// end
US>string name;
public HogeClass (string name)
{
this.name = name;
}
//def instance_method_name(argument)
public void instance_method(object argument)
{
// # Call Class Method
class_method_name();
Use the Add
method to add to the list.
//#Add value to class variable
// @@class_variable<<arg
class_variable.Add(argument);
Console output can be done via Console.WriteLine
, but the display is different from Ruby, so you may not get the desired output without using foreach
or string.Join
.
//# Reference class variables to output
// p@@class_variable
Console.WriteLine(class_variable);
// # Output referencing instance variables
// p@instance_variable
Console.WriteLine(instance_variable);
// end
}
As for other signatures, be aware that C# must specify modifiers for each member, such as public
/private
.If you omit the access modifier, it will be treated as private
.
//# class method
// def self.class_method_name
// end
public static void class_method_name()
{
}
// private
// # private method
// default_method_name
// end
private void private_method_name()
{
}
}
Regarding the void
in question, this is the type of return value.The method definition in C# must precede the method name with the type of value the method returns.
public int IntegerMethod()
{
return1234;
}
static string TextMethod()
{
return "1234";
}
A special example of the return value is the void
keyword when the method does not return a value.
There is no keyword directly equivalent to Ruby's def
because C# recognizes the method when it declares the type name + method name + argument as described above.
The full 1:1 translation is written by pgrho, so please do not do anything else.
You are free to name, but there are name guidelines, so it is recommended that you follow them
Also, C# requires explicit typing for everything, for example, name is the string
type (probably because it contains a string).You can also use ArrayList
where you can also store any type of array, but you typically use List<T>
.Hashtable
is also Dictionary<TKEy,TValue>
.
In addition to public
/private
, C# also has the access qualifier, so make sure to select it appropriately.
© 2024 OneMinuteCode. All rights reserved.