1. Anasayfa
  2. Visual Studio C#

C# Not All Code Paths Return A Value C# Hatası

C# Not All Code Paths Return A Value C# Hatası
0

Not All Code Paths Return A Value hatası genelde geriye değer gönderen metotlarda return ifadesinin yanlış, eksik veya fazla yazılmasında dolayı oluşur. hemen bir örnek yapalım..

static void Main(string[] args)
        {
            Console.WriteLine("Birinci Sayı");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("İkinci Sayı");
             int b = Convert.ToInt32(Console.ReadLine());
             Console.WriteLine("İşlem (+,-,*,/)");
            string c = Console.ReadLine();
            Console.WriteLine("Sonuç= "+islem(a,b,c));
            Console.ReadKey();
        }
        static int islem(int s1, int s2, string d)
        {
            if (d == "+")
            {
                return s1+s2;
            }                      
        }

notallcodepathsreturnavaluec
bu kodları yazıp çalıştırdığımız zaman “(int, int, string)’: not all code paths return a value” şeklinde bir hata mesajı alacağız çünkü islem metodunda kullanılan if koşullarının sağlanamama olasılığı olabileceğinden dolayı bir hata veriyor.
hatayı gidermek için kodlamayı aşağıdaki gib değiştiriyoruz.

 static void Main(string[] args)
        {
            Console.WriteLine("Birinci Sayı");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("İkinci Sayı");
             int b = Convert.ToInt32(Console.ReadLine());
             Console.WriteLine("İşlem (+,-,*,/)");
            string c = Console.ReadLine();
            Console.WriteLine("Sonuç= "+islem(a,b,c));
            Console.ReadKey();
        }
        static int islem(int s1, int s2, string d)
        {
            if (d == "+")
            {
                return s1 + s2;
            }
            else if (d=="*")
            {
                return s1 * s2;
            }
            else
            {
                return 0;
            }           
                    }
Bu İçeriğe Tepkin Ne Oldu?
  • 1
    ba_ar_l_
    Başarılı
  • 0
    gayet_yi
    Gayet İyi
  • 2
    te_ekk_rler
    Teşekkürler
  • 1
    anlamad_m
    Anlamadım
  • 1
    yetersiz
    Yetersiz
İlginizi Çekebilir
Subscribe
Bildir
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Yorum
Inline Feedbacks
View all comments