``` Home All Posts Tags About Atom feed C# Programming Overview (2) 2d tutorial game development xna Continued from previous post 8. User Input An address is a number that is assigned to each byte in a computer’s memory. It is used to track where data and instructions are stored in memory. Each byte is assigned an address whether or not it is being used to store data. Facts: Instead of using the assignment operator to assign value to the variables, the input function ReadLine is used. This is an arithmetic expression. class test { static void Main() { int i,j; Console.Write("Enter i: "); i = Int32.Parse(Console.ReadLine()); Console.Write("Enter j: "); j = Int32.Parse(Console.ReadLine()); Console.WriteLine(Add(i,j)); } static int Add(int x, int y) { return x+y; } } Facts: Four variables of type integer are declared, defined, and assigned a value. int x1=2,y1=1,x2=7,y2=3; In C#, you can assign an initial value to a variable during the declaration. The function Distance takes four integer arguments and returns a value of type double. The C# double type allows the variable to have a value with fraction. A value with fraction is made from an integral part followed by a decimal point followed by the precision (5.3851648071345). The function Distance requires a square root calculation. The square root function Sqrt is defined in the namespace called Math. The function Distance declares and defines six variables: x1,y1,x2,y2,deltaX, and deltaY. Remember that variables declared inside a function are only available while executing the function. deltaX and deltaY are used to hold the difference between the first and the second point. The statement deltaX=x2-x1; subtract x1 from x2 then assign the result to deltaX. In C# arithmetic, the multiplication * operator is evaluated from left to right. The multiplication operator has a higher order of precedence than the addition operator. This is why deltaX*deltaX is evaluated first to 25. Next deltaY*deltaY is evaluated to 4. Then 25 and 4 are added evaluating to 29. Then the square root of 29 is evaluated to 5.3851648071345. 9. Conditional Expression class test { static void Main() { int i; string s; Console.WriteLine("Enter a number then press return:"); i = Int32.Parse(Console.ReadLine()); if(i>100) { Console.WriteLine("{0} is larger than 100", i); } else { Console.WriteLine("{0} is less or equal than 100",i); } } } Facts: i>100 is the Boolean expression that evaluates to true or false. If the expression is true, then the statement (or the block of statements enclosed between an opening and a closing curly braces) following the condition is executed. If the expression is false then the statement following the condition is skipped. The statement following the else is executed only when the conditional expression is false. In other words, only one of the WriteLine statements will be executed. The conditional statement starts with the keyword if followed by an opening parenthesis, followed by a Boolean expression, followed by a closing parenthesis, followed by an instruction: if(Boolean expression) instruction; Notice that there is no semicolon after the closing parenthesis of the Boolean expression because the conditional statement has not ended yet. 10. Loops class test { static void Main() { int i; for(i=0;i<10;i=i+1) { Console.Write("{0} ",i); } Console.Write("\n"); i=0; while(i<10) { Console.Write("{0} ",i); i=i+1; } } } Facts: The for loop form is: for (expression1 ;expression2 ;expression3 ) statement The loop is initialized through expression1 i=0; Expression2 specifies the test made before each iteration i<10; If expression2 is true, the statement Console.Write(“{0} “,i); is executed, then expression 3 i=i+1 is executed. The loop iterates until expression2 is false. If expression2 is false, the for loop will exit, and the control is transferred to the statement following the statement. Expression3 is evaluated after each iteration. Any or all of the three for expressions may be omitted, but the semicolon must remain. The while loop form is: while (expression) statement If expression i<10 is true, the statement is executed until expression becomes false. In our case the statement is made from a block enclosed between curly braces: { Console.WriteLine(“{0} “,i); i=i+1; } If expression is false, the execution resumes at the following statement. In our case, the following statement is the end of the program. The expression is evaluated before the statement is executed. When the expression is false from the first time, the statement will never be executed. 11. One-Dimensional Array class test { static void Main() { int i; int[] A = new int[10]; for(i=0;i<10;i=i+1) { A=i; } for(i=0;i<10;i=i+1) { Console.Write("{0} ",A); } } } Facts: It is a collection of variables of the same type that are referred to by the same name. In our case int[] A = new int[10]; reserved 10 integers. Array elements are accessed through an index; in our case the index is i. The first element is accessed by index 0 A[0]. The highest address corresponds to the last element and it is accessed by index (total number of elements – 1); in our case it is A[9]. The amount of storage required to hold an array depends on the type and the total number of elements; in our case it is 10 * 4=40, since each integer is 4 bytes. The C# compiler does not perform index range checking. The array element is accessed by indexing the array name. It is done by writing the index enclosed between brackets placed after the array name. arrayName[index] A=i; 12. Structure class test { struct point { public int x; public int y; } static void Main() { point p1, p2; p1.x=2; p1.y=1; p2.x=7; p2.y=3; Console.WriteLine(Distance(p1,p2)); // will print 5.3851648071345 } static double Distance(point p1,point p2) { int deltaX, deltaY; deltaX=p2.x - p1.x; deltaY=p2.y - p1.y; return Math.Sqrt(deltaX*deltaX + deltaY*deltaY); } } Facts: A structure is an object consisting of a sequence of named members of various types. It is a collection of variables referenced under one name. The collection of variables is logically related. It provides a convenient way in order to keep related information together. A structure is declared by typing the keyword struct, followed by the structure name, followed by the structure members enclosed between curly braces; for example: struct point { public int x, y; } Once a structure is declared, variables having the structure type could be declared by typing the structure name followed by the variable name; for example: 1: int deltaX; The dot “.” operator is used to access the structure member. First write the structure variable name, followed by a dot, followed by a member; for example: deltaX=p2.x - p1.x; Conclusion Well that is the overview done, next comes the full detail for the above. As before read only if you wish but for beginners, I’d highly recommend it (also also check out the docs on the MSDN library which has a vast amount of information and free samples for the above and so much more. More to come Simon (Darkside) Jackson 2d tutorial game development xna Simon (darkside) Jackson Engineer, industry executive, research enthusiast. Avid learner with diverse interests in coding, game development, Mixed Reality (AR/VR/XR) and reinforcement learning. 25+ years of experience working in multinational corporations and startups. Recent posts .NET 8, meet MonoGame MonoGame on the Web, No Really! GameDev jamming with MonoGame MonoGame, the order of things! Newsletter Subscribe here to get our latest updates Email Address Comments   Write a comment ... 2024 © Simon (darkside) Jackson Books Posts Tags About Privacy Policy (function() { var sw = document.getElementById('theme-switch'); var html = document.getElementsByTagName('html')[0]; var logo = document.getElementById('logo'); var nightModeOption = ('auto' || 'auto').toLowerCase(); var storage = nightModeOption === 'manual' ? localStorage : sessionStorage; var themeData = loadThemeData(); function saveThemeData(data) { storage.setItem('theme', JSON.stringify(data)); } function loadThemeData() { var data = storage.getItem('theme'); try { data = JSON.parse(data ? data : ''); } catch(e) { data = { nightShift: undefined, autoToggleAt: 0 }; saveThemeData(data); } return data; } function handleThemeToggle(nightShift) { themeData.nightShift = nightShift; saveThemeData(themeData); html.dataset.theme = nightShift ? 'dark' : 'light'; if (nightShift) { logo.setAttribute("src", "./assets/img/branding/darkgenesis-logo-full-dark.png"); } else { logo.setAttribute("src", "./assets/img/branding/darkgenesis-logo-full-light.png"); } setTimeout(function() { sw.checked = nightShift ? true : false; }, 50); } function autoThemeToggle() { // Next time point of theme toggle var now = new Date(); var toggleAt = new Date(); var hours = now.getHours(); var nightShift = hours >= 19 || hours <=7; if (nightShift) { if (hours > 7) { toggleAt.setDate(toggleAt.getDate() + 1); } toggleAt.setHours(7); } else { toggleAt.setHours(19); } toggleAt.setMinutes(0); toggleAt.setSeconds(0); toggleAt.setMilliseconds(0) var delay = toggleAt.getTime() - now.getTime(); // auto toggle theme mode setTimeout(function() { handleThemeToggle(!nightShift); }, delay); return { nightShift: nightShift, toggleAt: toggleAt.getTime() }; } // Listen the theme toggle event sw.addEventListener('change', function(event) { handleThemeToggle(event.target.checked); }); if (nightModeOption == 'auto') { var data = autoThemeToggle(); // Toggle theme by local setting if (data.toggleAt > themeData.autoToggleAt) { themeData.autoToggleAt = data.toggleAt; handleThemeToggle(data.nightShift); } else { handleThemeToggle(themeData.nightShift); } } else if (nightModeOption == 'manual') { handleThemeToggle(themeData.nightShift); } else { var nightShift = themeData.nightShift; if (nightShift === undefined) { nightShift = nightModeOption === 'on'; } handleThemeToggle(nightShift); } })(); function toggle_comments(){ var commentCurtain = document.getElementById('comment-curtain') if (commentCurtain) { commentCurtain.classList.toggle('hide') } var disqusThread = document.getElementById('comment-layout') if (disqusThread) { disqusThread.classList.toggle('show') } } function copyToClipboard() { navigator.clipboard.writeText('https://simondarksidej.github.io/c-programming-overview-2.html').then(function() { alerts = document.getElementsByClassName('alert') for (i=0; i < alerts.length; i++){ alerts[i].innerHTML='\u00ABlink copied\u00BB'; setTimeout((function(i){ return function(){alerts[i].innerHTML='';}})(i), 1600 ); }; }, function() { prompt("Unable to copy, please use this link:", "https://simondarksidej.github.io/c-programming-overview-2.html"); }); } \$(function () { if (document.getElementById('comment-curtain') == null){ var disqusThread = document.getElementById('comment-layout') if (disqusThread) { disqusThread.classList.toggle('show') } } var tweetTags = document.getElementsByTagName("tweet"); if (tweetTags != null){ for (i=0; i<tweetTags.length; i++){ tweetA = document.createElement("a") tweetA.href = 'https://twitter.com/share?text=' + encodeURIComponent(tweetTags[i].textContent) + '&via=simondarksidej&url=' + window.location.href; tweetA.target = "_blank"; tweetA.className = 'twitter'; tweetSpanText = document.createElement('span'); tweetSpanText.className = 'tweetText'; tweetSpanText.appendChild(document.createTextNode(tweetTags[i].textContent)); tweetSpanIcon = document.createElement('span'); tweetSpanIcon.className = 'tweetIcon'; tweetSpanIcon.appendChild(document.createTextNode("click to tweet")); tweetI = document.createElement("i"); tweetI.className = 'fa fa-twitter'; tweetSpanIcon.appendChild(tweetI); tweetA.appendChild(tweetSpanText); tweetA.appendChild(tweetSpanIcon); tweetTags[i].textContent = ""; tweetTags[i].appendChild(tweetA); } } }); !function(c,h,i,m,p){m=c.createElement(h),p=c.getElementsByTagName(h)[0],m.async=1,m.src=i,p.parentNode.insertBefore(m,p)}(document,"script","https://chimpstatic.com/mcjs-connected/js/users/8ece198b3eb260e6838461a60/d20d9fb9aad962399025da52e.js"); SimpleJekyllSearch({ searchInput: document.getElementById('search-input'), resultsContainer: document.getElementById('results-container'), json: './search.json', searchResultTemplate: '<li><a href="{url}" title="{description}">{title}</a><p>{description}</p></li>', noResultsText: 'No results found', fuzzy: false, exclude: ['Welcome'] }); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-S2P4W0VHVB'); ```