What is the correct syntax for using object orientation in C # using .NET Core? [closed]

-4

I have to familiarize myself with implementation of object-oriented code in C # development. However, the materials I am learning assume that I have Windows and Visual Studio environment installed ( example ). When I search for material about it I end up with object-oriented tutorials that have little to do with my specific problem (which is to do this using .NET Core).

So I guess I should have some import (in C # they use the expression using + to import), anything that allows me to just environment myself to use OOP.

I'm trying to implement with the following code:

using System;

namespace hwapp
{
    class Program
    {
        static void Main(string[] args)
        {
           objeto c = new Program();
    c.numero = 12;
    c.titular = "João";
    c.saldo = 102;

    Console.WriteLine("Número: "+c.numero+"\n");
    Console.WriteLine("Titular da conta: "+c.titular+"\n");
    Console.WriteLine("Salto: "+c.saldo+"\n");
        }
    }
}

When giving the command dotnet run , returns the following message

  

Program.cs (9,12): error CS0246: The type or namespace name 'object' could not be found [/ home / upgrade / dotnet / hwapp / hwapp.csproj]

     

The build failed. Please fix the build errors and run again.

Recommended readings:

What can I do in .Net Framework and .Net Core not? And vice versa

link

    
asked by anonymous 14.05.2017 / 16:44

2 answers

9

Any material that teaches a language on top of an IDE is not good language learning material. I do not even like the stuff you're wearing, but it's just my opinion.

OC # is the same language no matter which runtime or operating system which is running. So learning the language with good material can use that knowledge identically everywhere. The .NET Core is just a differently written basis for using the same thing. Of course, non-basic libraries may have some differences to suit your goal that is different from the .NET Framework. And you can not do something universal like some people believed or believe until today.

Object orientation is just a code organization. Either you know OOP or you do not know, this is not language. And I must say that few people really know what it is and how to do OO correctly, most just think they know, see Dunning-Kugger effect . Object orientation is just a secondary paradigm.

The displayed code has nothing close to OOP. It actually has a typo, the type should be object and not objeto . Anyway this should not be of type object . This code is absolutely procedural and meaningless. because it accesses members that do not exist in type object .

I recommend choosing a material that teaches in steps, that only enter into object orientation when mastering the basics. See .

This will probably cause more confusion than help, but something that would be in line with what you are wanting:

using static System.Console;

public class Program {
    public static void Main(string[] args) {
        var conta = new Conta(1, "Joao", 100M);
        WriteLine(conta.Sacar(103.45M) ? $"Saque realizado, saldo restante {conta.Saldo}" : "Não foi possível sacar");
        conta.Depositar(50.0M);
        conta.Titular = "Joao da Silva";
        WriteLine(conta.Sacar(103.45M) ? $"Saque realizado, saldo restante {conta.Saldo}" : "Não foi possível sacar");
        WriteLine($"Número: {conta.Numero}");
        WriteLine($"Titular da conta: {conta.Titular}");
        WriteLine($"Saldo: {conta.Saldo}");
    }
}

public class Conta {
    public int Numero { get; set; }
    public string Titular { get; set; }
    public decimal Saldo { get; set; }
    public Conta(int numero, string titular, decimal saldo) {
        Numero = numero;
        Titular = titular;
        Saldo = saldo;
    }
    public void Depositar(decimal valor) => Saldo += valor;

    public bool Sacar(decimal valor) {
        if (Saldo - valor >= 0) {
            Saldo -= valor;
            return true;
        }
        return false;
    }
}

Obviously this is a huge simplification. A real class would have much more complex. Is this object-oriented? Partly. There is encapsulation, and only. OOP is much more complex than that, but in a simplified way, starting from what does not need other complexities can be considered OO.

So the error here is exclusively typing and misuse of fields, it has nothing to do with being on Linux, being .NET Core, or using OOP. There is a problem with the code being very bad, especially if you are wanting to do OOP.

See running on .NET Fiddle . And No Coding Ground . Also put it on GitHub for future reference .

About using you need to know what it serves . It makes no sense to import names from something that is part of the language, not the library.

It's important to note that the AP's answer holds the same wrong assumptions as the question and incorporates a few more . It is normal when the person is unaware of the facts, but is alert to those who are learning.

14.05.2017 / 16:59
-6

The issue has been solved with the questions below.

C # errors

  

Then I imagine that there should be some import (in C # use the expression   "using" + import), anything that allows me to just   environment to use OOP.

The answer is no! No additional import / library / framework is required to test features of the object-oriented paradigm. C # natively works object-oriented, and the failure of the test performed has to do with the lack of knowledge of the AP regarding the implementation of C # with this paradigm, and use of swelling courseware (which limits to certain development environment). The errors will be commented below.

static void Main(string[] args)
        {
           objeto c = new Program();
    c.numero = 12;
    c.titular = "João";
    c.saldo = 102;

In the third line, the code is following the example of the didactic material.

Conta c = new Conta();

But even in the courseware, the Account class had already declared the type of its attributes.

class Conta
{
    int numero;
    string titular;
    double saldo;
}

The main error was not declaring the types of attributes in the sample code. This bug has been fixed by StackOverFlow in English.

using System;

namespace hwapp {
    class Program {
        // Aqui embaixo está a correção dos atributos
        int numero;
        string titular;
        int saldo;

        static void Main(string[] args) {
            var conta = new Program(); // linha corrigida
            conta.numero = 12;
            conta.titular = "João";
            conta.saldo = 102;

            Console.WriteLine("Número: "+conta.numero.ToString()+"\n");
            Console.WriteLine("Titular da conta: "+conta.titular+"\n");
            Console.WriteLine("Saldo: "+conta.saldo.ToString()+"\n");
        }
    }
}

Object-oriented paradigm conceptualization errors

About this line ..

objeto c = new Program();

Even if "object" was the actual object, it was not "set", so it does not exist. The explanation why the didactic material did this has already been said above. It is worth mentioning that the AP had no intention of using the "object" syntax, which I did not know existed in C #. Another problem is the original title of the question:

  

How to program object orientation in C # using .NET CORE?

Although the question is still programming, it can induce the user of the SOPT to lecture on the Object Oriented paradigm (the concept), not the implementation problem (the code). For example, questions can be raised if a code that does not use any of the pillars of the OOP paradigm (abstraction, encapsulation, inheritance, and polymorphism) is actually OOP. A purist advocate of the paradigm may claim that there must be full use of the pillars. Your opponent will claim that excess inheritance causes coupling. In the end neither will approach the implementation in C # (which always has differences between one language and another). For this reason, after editing this post, the title of the question and parts of the content will be changed. Still about a third AP error involving OOP, it is to suppose that there should be an additional one anyway. This error was also cited in the C # errors. If such a tool claims that it natively supports OOP, at least any additional should not be installed / imported / etc. Perhaps this "bizarre" exists in some tool but is subject to criticism. Below code of the class running without import.

namespace hwapp {
    class Program {
        // Aqui embaixo está a correção dos atributos
        int numero;
        string titular;
        int saldo;

        static void Main(string[] args) {
            var conta = new Program(); // linha corrigida
            conta.numero = 12;
            conta.titular = "João";
            conta.saldo = 102;

        }
    }
}

The maximum that happens is a Data Warning not being used

Program.cs(6,13): warning CS0414: The field 'Program.numero' is assigned but its value is never used [/home/upgrade/dotnet/hwapp/hwapp.csproj]
Program.cs(8,13): warning CS0414: The field 'Program.saldo' is assigned but its value is never used [/home/upgrade/dotnet/hwapp/hwapp.csproj]
Program.cs(7,16): warning CS0414: The field 'Program.titular' is assigned but its value is never used [/home/upgrade/dotnet/hwapp/hwapp.csproj]

About a fourth error involving this topic, is in the concept of CamelCase. This is not necessarily an OOP bug, but a good one. But that induced an OOP error by not noticing the syntax of the Account class.

The problem was solved by seeking information from these concepts.

StackOverFlow response in English:

link

Object orientation

link

About CamelCase

link

    
14.05.2017 / 20:02