Autor Beitrag
12profi
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: Sa 14.08.10 20:18 
Hallo,kann mir jemand das checked in c# erklären weil z.b. mit oder ohne checked ist es das gleiche :


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
static void Main(string[] args)
        {
            // Zahleneingabe anfordern 
            Console.Write("Geben Sie eine Zahl im Bereich von ");
            Console.Write("0...{0} ein: ", Int16.MaxValue);
            // Eingabe einem short-Typ zuweisen 
            short shtVar = Convert.ToInt16(Console.ReadLine());
            // Überlaufüberprüfung einschalten 
            byte byteVar = checked((byte)shtVar);
            Console.WriteLine(byteVar);

            
        }

und ohne gibt es kein Unterschied wenn ich das Programm ausführe oder?

Ich hoffe ihr könnt mir helfen ;)
mfg 12profi

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#
gfoidl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 157
Erhaltene Danke: 19

Win XP
C#, Fortran 95 - Visual Studio
BeitragVerfasst: Sa 14.08.10 20:53 
Hallo,

siehe zB hier.


mfG Gü

_________________
Alle sagten, das geht nicht! Dann kam einer, der wusste das nicht - und hat's gemacht!
12profi Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: Sa 14.08.10 22:28 
Hi,
Danke das habe ich auch versucht nur leider verstehe ich das nicht (zu kompliziert für ein 12jährigen Jungen) desewegen wollte ich das mir das jemand mit eigenen Wirten erklärt

mfg 12profi

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4799
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: So 15.08.10 10:15 
Wenn du jetzt zur Laufzeit einen Wert dort angibst, der nicht mehr in ein 'byte' (0 bis 255) paßt, dann kriegst du eine OverflowException (ohne das checked würde einfach bei short->byte das obere Byte ignoriert).

P.S: Der Compiler kann Fehler bei "checked" nur erkennen, wenn es sich innerhalb der Klammern um konstante Ausdrücke handelt, z.B.
ausblenden C#-Quelltext
1:
2:
3:
const short shVar = 1000// als Klassenmember

byte byteVar = checked((byte)shVar);
12profi Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: So 15.08.10 10:20 
Achso das heißt:Mit checked checkd er ob es Konvertiert werden kann und gibt mir fals es falsch ist einen Lauftzeitfehler an. Ahhhhh Danke

mfg 12profi

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#
Greenberet
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 339
Erhaltene Danke: 20

Win 10
C# (VS 2012), C++ (VS 2012/GCC), PAWN(Notepad++), Java(NetBeans)
BeitragVerfasst: So 15.08.10 11:20 
user profile icon12profi hat folgendes geschrieben Zum zitierten Posting springen:
Achso das heißt:Mit checked checkd er ob es Konvertiert werden kann und gibt mir fals es falsch ist einen Lauftzeitfehler an. Ahhhhh Danke

mfg 12profi


Nicht nur beim Konvertieren. auch wenn du z.b. int.MaxValue + 1 machst. Ohne würdest du auf int.MinValue kommen und mit checked bekommst du eine Exception
12profi Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: So 15.08.10 12:34 
Was ist das eig.Exception

Mfg 12profi

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 15.08.10 12:44 
user profile icon12profi hat folgendes geschrieben Zum zitierten Posting springen:
Was ist das eig.Exception

Wie bitte?
12profi Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: So 15.08.10 13:04 
user profile iconLuckie hat folgendes geschrieben Zum zitierten Posting springen:
user profile icon12profi hat folgendes geschrieben Zum zitierten Posting springen:
Was ist das eig.Exception

Wie bitte?

was ist eine Exception?

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 15.08.10 13:38 
Nutz doch einfach mal Google. Oder such hier im Forum. Oder guck in dein Buch.
12profi Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: So 15.08.10 15:50 
Ok,ok danke

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#
Programmierer
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 15.08.10 16:04 
12profi Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: So 15.08.10 16:28 
^^ Danke hab ich schon nachgekugt

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#