What is the correct syntax to use C# object orientation using . NET Core?

Asked

Viewed 281 times

-4

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

So I guess there must be some import (in C# they use the expression using + import), anything that allows me to just 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");
        }
    }
}

In 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 (are you Missing a using Directive or an Assembly Reference?) [/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

https://docs.microsoft.com/pt-br/dotnet/articles/core/

2 answers

9

Any material that teaches a language on top of an IDE is not a good material to learn language. I don’t even like the material you are using, but it’s just my opinion.

C# is the same language no matter in which Runtime or operating system that is running. So if you learn the language with good material you can use that knowledge in an identical way everywhere. The .NET Core is just a base written differently to use the same thing. Of course the libraries basic may have some differences to suit the purpose of it that is different from . NET Framework (who died). And you can’t do something universal like some people believed or believe until today that gives.

Object orientation is just a code organization. Either you know OOP or you don’t, it’s language-independent. And I must say that few people really know what it is and how to do it properly, most just think they know, see dunning-kugger effect. Object orientation is only a secondary paradigm.

The code shown is nowhere near the OOP. Actually there is a typo, the type should be object and not objeto. Anyway that shouldn’t be the type object. This code is absolutely procedural and meaningless. because it accesses members that do not exist in the type object.

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

This will probably cause more confusion than helping, 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;
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Obviously this is a huge simplification. A real class would be much more complex. Is this object-oriented? In part. There’s encapsulation, and that’s it. OOP is much more complex than this, but in a simplified way, assuming that it does not need other complexities can be considered OO.

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

About the using you need to know what it’s for. It makes no sense to import names of something that is part of the language and not the library.

Important to note that the AP answer keeps the same wrong premises of the question and incorporates a few more. It is normal when the person does not know the facts, but is alert to who is learning.

  • The point is I want to practice the exercises. I want to see it running. I just can’t do it in . NET CORE . E still has the problem of not everything used in . NET framework being used in . NET CORE.

  • When you return, I would like you to complement it with the answer of the English stackoverflow: http://stackoverflow.com/questions/43965452/how-to-program-in-oop-by-sharp-in-net-core/43965612#43965612 I also need lunch :)

  • 2

    The first comment does not make sense, it has no relevance to the problem. The answer there solves the problem, but desensina. Follow that and you’ll do wrong forever.

  • True! But luckily I discovered two things wrong with regard to both C# and OOP, which I did with that implementation. After lunch I will describe this, and it may help those who are learning and going through the same problem.

  • I just saw the end of the edition of your post. When I return I show why there is more than typo.

  • Yeah, there’s the use of local variables like fields, which I say at first.

  • There’s more to it than that. Soon I’ll put!

  • 2

    "Object orientation is just a code organization" +1. And if you could give another by the mention of D/K.

  • @Onosendai that turned out to be the most important thing of all answer :D

Show 4 more comments

-6

The problem has been solved with the questions below.

Errors involving C#

So I guess there must be some import (in C# they use the expression "using" + import), anything that allows me to just environment to use OOP.

The answer is nay! No additional import/library/framework is required to test object-oriented paradigm resources. The C# natively works oriented to objects, and the failure of the test performed has to do with the lack of knowledge of the PC regarding the implementation of C# with this paradigm, and use of swollen didactic material (which limits to a 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 teaching material, 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 attribute types in the sample code. This defect was corrected 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");
        }
    }
}

Errors of conceptualization of the object-oriented paradigm

On that line..

objeto c = new Program();

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

How to program orientation to C# objects using . NET CORE?

Although it is still programming the question, it can induce the SOPT user to lecture on the Object Orientation paradigm (the concept), and not the implementation problem (the code). For example, questions may be raised whether a code that does not use any of the pillars of the OOP paradigm (abstraction, encapsulation, inheritance and polymorphism) is in fact OOP. A purist proponent of the paradigm may argue that there must be full use of the pillars. Your opponent will claim that excess inheritance causes coupling. In the end neither of the two will address the implementation in C# (which always has differences between one language and another). For this reason, after the publication of this post, the title of the question and parts of the content will be changed. Still on a third PA error involving OOP, it is assumed that there should be an additional one anyway. This error was also cited in errors involving C#. If such a tool claims to natively support OOP, at least any additional should not be installed/imported/etc. Perhaps this "bizarre" exists in some tool but is open to criticism. Below code of the working class 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 most that happens is a Warning of data 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]

On a fourth error involving this topic, it is in the concept of Camelcase. This is not necessarily an error of OOP, but of good practice. But that induced an OOP error by not noticing the syntax of the Account class.

The problem was solved by seeking information on these concepts.

Response from Stackoverflow in English:

https://stackoverflow.com/questions/43965452/how-to-program-in-oop-by-c-sharp-in-net-core

Object orientation

https://msdn.microsoft.com/pt-br/library/cc580626.aspx?f=255&MSPPError=-2147217396

About Camelcase

https://en.wikipedia.org/wiki/Camel_case

  • 1

    Practically none of this makes sense, and there are several mistakes, inducing those who do not know to think that everything there is true, which is very little. So if the problems of this answer are not solved, I will be forced to negative to prevent people from making mistakes.

  • You can use the reply or comment session to point out these errors, and eventually they can be fixed (if they exist). However, it was these issues that solved my problem, partly in Stackoverflow in English, partly by reflecting on C# issues and using official documentation. So much so that I can continue the exercises of the mentioned material without problems. I can’t comment to you about object orientation, because it’s an extensive subject, and it’s outside the scope of the site. Already the code problem has been solved.

  • 9

    Not because here is not a forum. If you want to learn everything wrong is your problem, the chance to learn right has been given. To tell you the truth, I didn’t even think this question was a good one, so I went out of my way to answer it as completely as possible. And I think I’ve already demonstrated that I know what I’m talking about, especially on a question as basic as this, and what I gain is a negative. Wonderful.

  • Here is no more forum also is no crystal ball. When any member cite any mistakes, I will naturally correct them. I have no problem with that. The negative (which was mine) has more to do with the proposed problem than with the coherence of the content. I don’t vote for a post because I think it’s good. I vote if it’s right about the problem. It is a different criterion that I use, unlike many members, and that is not a problem for me. You know the subject well, and that is commendable. But unfortunately it has no practical relationship with the problem cited.

  • Read my answer, it’s all there what’s wrong. Not correct is your right. Precisely because it is not a crystal ball, only you could respond the way you wanted. I answered what is useful for everyone. My answer solves 100% of the problems reported in the question, does not solve what is not in the question.

  • I’ll leave it open. Because your post didn’t help much to solve my problem (which has already been solved, with other information). Those who go through the same problem will decide. At least your question issue turned out well. Posting got less prolific.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.