R
R
Rashad2020-07-04 13:32:36
C++ / C#
Rashad, 2020-07-04 13:32:36

Methods and functions in C#?

Hello!

I am reading Schildt now and have reached the methods (Chapter 6). In general, such a question arose, but in C # methods and functions are somehow different or not? Because, for example, in JS (and not only) functions look like this:

function test() {
    // code...
}

and in C#:
static void test() {
    // code...
}


Maybe I don't understand something, but isn't the syntax of functions and methods in C# the same? And what are called methods and functions? Thanks for answers.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Victor Bomberow, 2020-07-04
@Rashad31

There are terms function, procedure, method within the framework of programming paradigms, but the terminology in C# is different.
There are no functions and procedures in C#, only methods and delegates.
Lambda expressions, depending on the context, "under the hood" are cast to anonymous delegates with a body or to Func, Predicate, Action.
Everything is stuck in the object model, which is supported by the abstract machine of the C # language.
The translation unit is a class, and there cannot be free functions in a namespace, just like function pointers in principle - delegates are used instead of function pointers in C#. The delegate is implemented as an abstract class, which made it possible to implement type-safe references to methods that can be added and subtracted, which made it possible to conveniently implement asynchrony.
The delegate only points to the signature of the method. But it is assumed that the object on which the method is called through the delegate is an instance of a class that has the same signature as the delegate.
In C#, there is no concept of a function as an object; a function cannot be created just like that. In general, it is possible, but before using it, it will need to be compiled, but in any case it will be in some class, and it will be a method of this class; and in order to call methods, you first need to get a reference to an instance of the class.

F
freeExec, 2020-07-04
@freeExec

Method - it does something silently and returns nothing ( void).
The function, after doing something, returns its result, whether it be a number, text, or even a whole class.
In general, the differences are only philosophical.

V
Vladimir Korotenko, 2020-07-04
@firedragon

Here is a good example. The compiler catches for me when I change the type to another in one class and just shows the string, in JS I would catch it myself
https://github.com/vkorotenko/FiasServer/blob/mast...

public static DbStead Get(this Stead s)
        {
            if (s.Number?.Length > 120)
#pragma warning disable 642
                ; // ловим превышение длины
#pragma warning restore 642
            return new DbStead
            {
                STEADID = s.STEADID,
                IFNSUL = s.IFNSUL.ToNullInt(),
                OKATO = s.OKATO.ToNullLong(),
                RegionCode = s.RegionCode,
                PostalCode = s.PostalCode.ToNullInt(),
                NORMDOC = s.NORMDOC.ToNullGuid(),
                OKTMO = s.OKTMO.ToNullLong(),
                TERRIFNSUL = s.TERRIFNSUL.ToNullInt(),
                IFNSFL = s.IFNSFL.ToNullInt(),
                TERRIFNSFL = s.TERRIFNSFL.ToNullInt(),
                STEADGUID = s.STEADGUID,
                CADNUM = s.CADNUM,
                DIVTYPE = s.DIVTYPE,
                ENDDATE = s.ENDDATE,
                LIVESTATUS = s.LIVESTATUS,
                NEXTID = s.NEXTID.ToNullGuid(),
                Number = s.Number,
                OPERSTATUS = s.OPERSTATUS,
                PARENTGUID = s.PARENTGUID.ToNullGuid(),
                PREVID = s.PREVID.ToNullGuid(),
                STARTDATE = s.STARTDATE,
                UPDATEDATE = s.UPDATEDATE
            };
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question